Question

I'm creating a plugin on creating a phonecall activity to update a field in the recipients contact entities. This plugin runs on Create and on Update of a phonecall activity.

On Create AND "new_targetfield" is null => Updates correctly

On Update AND "new_targetfield" not null => Updates correctly

On Update AND "new_targetfield" is null => Nothing happens

I tried running the Plugin Profiler but I keep getting an error:

 <ErrorCode>-2147220970</ErrorCode>
 <ErrorDetails xmlns:d2p1="http://schemas.datacontract.org/2004/07/System.Collections.Generic

Here's a part of my code :

        IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

        IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
        IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

        if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
        {
            Entity entity = context.InputParameters["Target"] as Entity;

            if (entity.LogicalName != "phonecall")
            {
                return;
            }


            DateTime activitydate=entity.GetAttributeValue<DateTime>("actualstart");
            if (activitydate ==null && context.MessageName =="Update")
            {
                activitydate=((Entity)context.PostEntityImages["PhoneCallPostImage"]).GetAttributeValue<DateTime>("actualstart");
            }


            if (activitydate != null)
            {

                // update recipients
                EntityCollection Recipients = entity.GetAttributeValue<EntityCollection>("to");

                if (Recipients == null && context.MessageName == "Update")
                {
                    Recipients = ((Entity)context.PostEntityImages["PhoneCallPostImage"]).GetAttributeValue<EntityCollection>("to");

                }

                if (Recipients != null)
                {
                    foreach (Entity recipient in Recipients.Entities)
                    {
                        EntityReference partyId = recipient.GetAttributeValue<EntityReference>("partyid");

                        if (partyId != null)
                        {

                            // get recipient id
                            if (partyId.LogicalName == "contact")
                            {
                                Guid contactid = partyId.Id;

                                // update the recipient Last Contacted with Phone call date 
                                string fetch = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
                                  <entity name='contact'>
                                    <attribute name='contactid' />
                                    <attribute name='new_targetfield' />
                                    <filter type='and'>
                                      <condition attribute='contactid' operator='eq' uitype='contact' value='"+contactid+@"' />
                                    </filter>
                                  </entity>
                                </fetch>";

                                EntityCollection result = service.RetrieveMultiple(new Microsoft.Xrm.Sdk.Query.FetchExpression(fetch));
                                if (result.Entities.Count > 0)
                                {
                                    DateTime lasttouched = result.Entities[0].GetAttributeValue<DateTime>("new_targetfield");
                                    if (lasttouched != null)
                                    {
                                        if (activitydate > lasttouched)
                                        {
                                            Entity contact = new Entity();
                                            contact.LogicalName = "contact";
                                            contact.Id = contactid;
                                            contact.Attributes = new AttributeCollection();
                                            contact.Attributes.Add("new_targetfield", activitydate);

                                            service.Update(contact);
                                        }
                                        else
                                        {
                                            continue;
                                        }
                                    }
                                    else
                                    {
                                        Entity contact = new Entity();
                                        contact.LogicalName = "contact";
                                        contact.Id = contactid;
                                        contact.Attributes = new AttributeCollection();
                                        contact.Attributes.Add("new_targetfield", activitydate);

                                        service.Update(contact);
                                    }



                                }

                            }
                            else
                            {
                                continue;
                            }
                        }
                        else
                        {
                            continue;
                        }

                    }
                }
Was it helpful?

Solution

The problem was that a DateTime variable will never be null but equal to the MinValue of DateTime. That's why parts of the code weren't approached.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top