web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Answered

Create a record in an child entity on update of certain field in parent entity using Plugin

(0) ShareShare
ReportReport
Posted on by 105

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

I have the same question (0)
  • Verified answer
    Mahendar Pal Profile Picture
    45,095 on at

    Hello,

    This type of error occurs when you are trying to get value of some properties which does not exists in the collection, so make sure to check field availability before getting any field ( you are already checking for some of them)

    for example

    if(customer9.Contains("orc_name"))

    c.Attributes["orc_name"] = customer9.Attributes["orc_name"];

  • Rits Profile Picture
    105 on at

    Thanks..it worked for me..I had missed that check..thanks again

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 170 Super User 2025 Season 2

#2
#ManoVerse Profile Picture

#ManoVerse 70

#3
Jimmy Passeti Profile Picture

Jimmy Passeti 50 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans