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

  • Rits Profile Picture
    Rits 105 on at
    RE: Create a record in an child entity on update of certain field in parent entity using Plugin

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

  • Verified answer
    Mahendar Pal Profile Picture
    Mahendar Pal 45,095 on at
    RE: Create a record in an child entity on update of certain field in parent entity using Plugin

    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"];

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

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Tip: Become a User Group leader!

Join the ranks of valued community UG leaders

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,321 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans