Michele Ietri Michele Ietri - 2 months ago 28
C# Question

Retrieving contact information from partylist field in phonecall entity on create

I'm working on a plugin written in c# for an online instance of dynamics crm 2016 update 1.

As said in title, i need to retrieve contact information from a "phonecall" entity created filling the "to" field. that's the code that's not working for me

#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


the problem it's that
activityParty.ActivityId.Id
is null, even if i'm sure that the field on CRM is being filled with data.
I tried the plugin in post and pre, but it isn't working in any way.

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)


someone knows how to fix this?

EDIT:
activityParty.ActivityId.Id
returns an Id that doesn't match with any id in contacts, any suggestion?

Answer

Try to replace line

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

with line

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