When we update any field(suppose company phone no) in parent entity. the old and new values should be stored in newly created record in child entity(old values and new values)
I have written a code in post syn operation on update. and used post and pre entity image but i am getting exception "the given key was not present in the dictionary" . By debugging I noticed it is not able to read post image value .How can i fix this issue. please guide
Below is my code
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Metadata;
using Microsoft.Xrm.Sdk.Query;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Part2No9
{
public class UpdateTrackCustomer : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
try
{
ITracingService tracingService =
(ITracingService)serviceProvider.GetService(typeof(ITracingService));
// Obtain the execution context from the service provider.
IPluginExecutionContext context = (IPluginExecutionContext)
serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory serviceFactory =
(IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
if (context.MessageName != "Update")//plugin shouldnt trigger any of event
return;
// The InputParameters collection contains all the data passed in the message request.
if (context.InputParameters.Contains("Target") &&
context.InputParameters["Target"] is Entity)
{
Entity customer9 = (Entity)context.InputParameters["Target"];
//entity check
if (customer9.LogicalName != "orc_customer9")
{
return;
}
if (customer9.Contains("orc_companyphone") && customer9.Attributes["orc_companyphone"] != null)
{
string oldphonevalue = string.Empty;
string newphonevalue = string.Empty;
string phoneno = string.Empty;
RetrieveAttributeRequest retrieveAttributeRequest = new RetrieveAttributeRequest
{
EntityLogicalName = "orc_customer9",
LogicalName = "orc_companyphone",
RetrieveAsIfPublished = true
};
RetrieveAttributeResponse retrieveAttributeResponse = (RetrieveAttributeResponse)service.Execute(retrieveAttributeRequest);
AttributeMetadata retrievedAttributeMetadata = retrieveAttributeResponse.AttributeMetadata;
phoneno = retrievedAttributeMetadata.DisplayName.UserLocalizedLabel.Label;
if (context.PreEntityImages.Contains("LeadTopicImage") && context.PreEntityImages["LeadTopicImage"] is Entity)
{
Entity preMessageImage = (Entity)context.PreEntityImages["LeadTopicImage"];
oldphonevalue = (string)preMessageImage.Attributes["orc_companyphone"];
}
if (context.PostEntityImages.Contains("LeadTopicImage") && context.PostEntityImages["LeadTopicImage"] is Entity)
{
Entity postMessageImage = (Entity)context.PostEntityImages["LeadTopicImage"];
newphonevalue = (string)postMessageImage.Attributes["orc_companyphone"];
}
if (context.Depth > 2)
service.Update(customer9);
if (newphonevalue != oldphonevalue)
{
Entity c = new Entity("orc_trackcustomer");
c.Attributes["orc_name"] = customer9.Attributes["orc_name"];
c.Attributes["orc_modifiedfield"] = phoneno;
c.Attributes["orc_oldvalue"] = oldphonevalue;
//c.Attributes["orc_newvalue"] = newphonevalue;
c.Attributes["orc_trackcustomerlookup"] = new EntityReference("orc_customer9", customer9.Id);
service.Create(c);
}
}
}
}
catch (Exception e)
{
throw new InvalidPluginExecutionException(e.Message);
}
}
}
}
Thanks