Quantcast
Channel: Microsoft Dynamics CRM Forum - Recent Threads
Viewing all articles
Browse latest Browse all 55831

Plugin Keeps changing lookup field

$
0
0

Hi, I've written a plugin that does two things: update a field for contacts, and update a lookup field for accounts.

My plugin works fine for the first part (updating field for contacts), but the behavior for the second part is strange. For this part, when a contact field is updated the plugin searches for the parent account and changes a lookup field (called new_primarycontact) to this contact.

While monitoring, I noticed the lookup field keeps changing to random contacts for some time and eventually stops at an incorrect contact. Here is the code:

//Get the Account and change the Primary Contact lookup field
var res = from a in ServiceContext.CreateQuery("account")
//where a.Id.Equals(postImageEntity["parentcustomerid"])
select a;

//Apply changes to the account
foreach (var a in res)
{
        Entity temp = (Entity)a; //temporary account entity
        Guid G = new Guid(postImageEntity.Id.ToString());
        temp["new_primarycontact"] = new EntityReference("contact", G);
                            
        ServiceContext.UpdateObject(temp);
        ServiceContext.SaveChanges();
}

I haven't been able to find a way for the linq query above to return a single account, so I am just applying this change to all accounts in my sandbox for testing in the meantime. But why does the lookup keep changing and eventually stop at a random contact?

Thanks in advance.


Viewing all articles
Browse latest Browse all 55831

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>