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.

//Get the Account and change the Primary Contact lookup field

varres2=fromainServiceContext.CreateQuery("account")

//where a.Id.Equals(postImageEntity["parentcustomerid"])

selecta;

//Apply changes to the account

foreach (varainres2)

{

//Guid ContactID = new Guid("{" + postImageEntity.Id.ToString() + "}"); //ContactID is a string

Entitytemp= (Entity)a; //temporary account entity

stringid=postImageEntity.Id.ToString();

//Guid G = new Guid("{EF223952-C878-E511-80ED-C4346BAC59DC}"); //Canadian Student1 - ACIS

GuidG=newGuid(id); //ContactID is a string

temp["new_test"] =postImageEntity.Id.ToString();

temp["new_primarycontactacis"] =newEntityReference("contact", G);

ServiceContext.UpdateObject(temp);

ServiceContext.SaveChanges();

}


Viewing all articles
Browse latest Browse all 55831

Trending Articles



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