Reputation: 61
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());
Upvotes: 0
Views: 936
Reputation: 5531
You could use the below code
Guid ContactId = ((EntityReference)retrievedLead.Attributes["parentaccountid"]).Id;
string PrimaryContact = ((EntityReference)retrievedLead.Attributes["parentaccountid"]).Name;
Upvotes: 1
Reputation: 22836
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;
Upvotes: 2