简体   繁体   中英

Get accountID when creating lead in Dynamics CRM

I have a plugin that triggers when I'm creating a lead and want to get the ID for the account the lead is related to parentaccountid . I'm able to print "subject" for the lead but when it comes to "parentaccountid" I get the message "Microsoft.Xrm.Sdk.EntityReference" , I'm guessing it's null ? Weird thing is that when I'm looking at the lead record in FetchXML Builder there's a value in parentaccountid .

Guid leadId = new Guid(context.OutputParameters["id"].ToString());

ColumnSet cols = new ColumnSet(
new String[] { "subject", "parentaccountid" });

var retrievedLead = service.Retrieve("lead", leadId, cols);
tracingService.Trace(retrievedLead["subject"].ToString());

var accountId = retrievedLead["parentaccountid"];

tracingService.Trace(accountId.ToString());

Use the below syntax to get the value.

EntityReference lookupRef = retrievedLead.GetAttributeValue<EntityReference>("parentaccountid");

if (lookupRef != null) 
      Guid accountId = lookupRef.Id;

Or

var accountId = ((EntityReference)retrievedLead["parentaccountid"]).Id;

You could use the below code

Guid ContactId = ((EntityReference)retrievedLead.Attributes["parentaccountid"]).Id;
string PrimaryContact   = ((EntityReference)retrievedLead.Attributes["parentaccountid"]).Name;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM