Hi, I am trying to have a plugin to update fields in contact with old values, but I got the error, what else I have missed?



if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
Entity entity = (Entity)context.InputParameters["Target"];
if (entity.LogicalName == "contact")
{
Entity contact = (Entity)context.InputParameters["Target"];
Entity preImageContact = (Entity)context.PreEntityImages["ContactPreImage"];
Entity postImageContact = (Entity)context.PostEntityImages["ContactPreImage"];
string preImagefullname = preImageContact.Attributes["fullname"].ToString();
string preImagetradename = preImageContact.Attributes["tradename"].ToString();
string postImagefullname = postImageContact.Attributes["fullname"].ToString(); ;
string postmagetradename = postImageContact.Attributes["tradename"].ToString();
tracingService.Trace("Pre-image fullname: {0}, Pre-image tradename: {1}", preImagefullname, preImagetradename);
tracingService.Trace("Post-image fullname: {0}, Post-image tradename: {1}", postImagefullname, postmagetradename);
Entity mycontact = new Entity("contact");
mycontact.Id = contact.Id;
mycontact["previousname"] = preImagefullname;
mycontact["previoustradename"] = preImagetradename;
mycontact["changedate"] = DateTime.Now;
service.Update(mycontact);
tracingService.Trace("updated");
}
}
*This post is locked for comments
I have the same question (0)