在创建时从电话实体中的 partylist 字段中检索联系信息

Retrieving contact information from partylist field in phonecall entity on create

我正在为 dynamics crm 2016 update 1 的在线实例开发一个用 c# 编写的插件。

如标题所述,我需要从填充 "to" 字段的 "phonecall" 实体中检索联系信息。那是对我不起作用的代码

#region Plugin execution
        try
        {
            tracingService.Trace("AutocompletePhonecalls: Plugin start.");
            XrmServiceContext svcContext = new XrmServiceContext(service);

            EntityCollection to = entity.GetAttributeValue<EntityCollection>("to");

            for (int i = 0; i < to.Entities.Count; i++)
            {
                ActivityParty activityParty = to[i].ToEntity<ActivityParty>();
                Contact contact = (from c in svcContext.ContactSet where c.Id.Equals(activityParty.ActivityId.Id) select c).FirstOrDefault();
            }
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message + "\n" + ex.InnerException.Message);
        }
        #endregion

问题是 activityParty.ActivityId.Id 为空,即使我确定 CRM 上的字段正在填充数据。 我在 post 和 pre 中尝试了该插件,但它无法正常工作。

the error i got from CRM

Object reference not set to an instance of an object.Detail: 
<OrganizationServiceFault xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/xrm/2011/Contracts">
  <ErrorCode>-2147220956</ErrorCode>
  <ErrorDetails xmlns:d2p1="http://schemas.datacontract.org/2004/07/System.Collections.Generic" />
  <Message>Unexpected exception from plug-in (Execute): CGN.Plugins.AutocompletePhonecalls: System.Exception: Exception has been thrown by the target of an invocation.
Object reference not set to an instance of an object.</Message>
  <Timestamp>2016-09-09T09:47:59.4457726Z</Timestamp>
  <InnerFault i:nil="true" />
  <TraceText>
AutocompletePhonecalls: Plugin start.
</TraceText>
</OrganizationServiceFault>

Server stack trace: 
   at System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime operation, ProxyRpc& rpc)
   at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
   at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs)
   at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
   at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)

Exception rethrown at [0]: 
   at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
   at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
   at Microsoft.Crm.Sandbox.ISandboxHost.ExecuteAndReturnTraceInfo(SandboxCallInfo callInfo, SandboxPluginExecutionContext requestContext, Guid pluginAssemblyId, Int32 sourceHash, String assemblyName, Guid pluginTypeId, String pluginTypeName, String pluginConfiguration, String pluginSecureConfig, String assemblyContents, Boolean returnTraceInfo)
   at Microsoft.Crm.Sandbox.SandboxPlugin.Execute(SandboxClient client, SandboxCallTracker callTracker, IExecutionContext requestContext, String assemblyContents, Boolean returnTraceInfo)
   at Microsoft.Crm.Sandbox.SandboxCodeUnit.Execute(IExecutionContext context)

有人知道如何解决这个问题吗?

编辑: activityParty.ActivityId.Id returns 与联系人中的任何 ID 都不匹配的 ID,有什么建议吗?

尝试替换行

Contact contact = (from c in svcContext.ContactSet where c.Id.Equals(activityParty.ActivityId.Id) select c).FirstOrDefault();

同线

Contact contact = (from c in svcContext.ContactSet where c.Id.Equals(activityParty.PartyId.Id) select c).FirstOrDefault();