在Dynamics CRM中创建潜在客户时获取accountID

时间:2017-09-15 09:32:26

标签: c# dynamics-crm

我有一个插件,当我创建lead并希望获得IDlead相关的帐户的parentaccountid时触发该插件。我能够为"subject"打印lead,但是当涉及"parentaccountid"时,我收到了"Microsoft.Xrm.Sdk.EntityReference"消息,我猜测它是' s null?奇怪的是,当我在FetchXML Builder中查看主要记录时,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());

2 个答案:

答案 0 :(得分:2)

使用以下语法获取值。

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

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

或者

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

答案 1 :(得分:1)

您可以使用以下代码

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