locked
Dynamics CRM: Logic for retrieving, updating and committing a record using SDK plugin registration tool. RRS feed

  • Question

  •  

    I have written a C# plugin for post-update on the child record. On the post-update the calculated total has to be updated to the parent.

    In this I am trying to calculate the total value in the parent entity based on the values updated in the child entity, which has rate and units fields in it.Please note that there are 3 fields rate, unit and total.On post-update the parent has to update the total record from the child. Here, total=rate x unit. The code builds fine.

     

    The child entity works fine. For the parent entity I need help with the logic for :

    1. retrieving the parent record

    2.  update the record

    3. finally it commit to the database for my task to be complete

    I new to Dynamics CRM. So, I am trying hard to figure out the logic. Any help is appreciated


     

    Updated code:

     

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    using System.Threading.Tasks;

    using Microsoft.Xrm.Sdk;

     

    namespace Postupdatepluginpjt

    {

        public class Postupdateplugin : IPlugin

        {

            public void Execute(IServiceProvider serviceProvider)

            {

     

                // Obtain the execution context from the service provider.

     

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

    `

                // Get a reference to the Organization service.

     

                IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));

     

                IOrganizationService service = factory.CreateOrganizationService(context.UserId);

     

                if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)

                {

                    Entity entity = (Entity)context.InputParameters["Target"];

     

                    if (entity.LogicalName == "abcevent_child")

                    {

                        if (entity.Attributes.Contains("abcevent_rate") && entity.Attributes.Contains("abcevent_unit") && entity.Attributes.Contains("abcevent_parentid"))

                        {

                            Double rate = (Double)entity.Attributes["abcevent_rate"];

                            Double unit = (Double)entity.Attributes["abcevent_unit"];

                            EntityReference parentRef = (EntityReference)entity.Attributes["abcevent_parentid"];

                 

                            Double total = rate * unit;

     

                            //To Do's - Retrieve parent record using parent.id

                            //Entity parent

                            //parentRef.LogicalName   (abcevent_parent)

                            //parentRef.Id

     

                            //Retrieve entity record where ID=parentRef.Id and entityLogicalName=parentRef.LogicalName

                            //RetrieveEntity request

                            //Entity parent = retireval response

     

                            //Update the total amount

                            Entity parent = new Entity();

                            parent["abcevent_total"] = total;

     

                            //Commit the update to database

                            //UpdateEntity Request

                        }

                    }

                }

            }

        }

    }

     

    Friday, October 24, 2014 8:44 PM

All replies

  • Hi,

    Why do you want to fetch the parent record details?

    If you update few field values , then already you have the parent record ID. That's enough to update the record.

    To update the record below is the code. In your plugin already you are getting the parent record ID.

     
      //Update the total amount in parent entity
                EntityReference parentRef = (EntityReference)entity.Attributes["abcevent_parentid"];
                Entity parentEnt = new Entity();
                parentEnt.LogicalName = parentRef.LogicalName;
                parentEnt.Id = parentRef.Id;
                parentEnt["abcevent_total"] = total;
                service.Update(parentEnt);

    Regards,

    Priya

    Saturday, October 25, 2014 6:30 AM
  • The parent ID will only be in the Target InputParameter if it has been changed in the initial operation, and this will often not be the case if the child entity is updated. Instead, register a PostEntityImage on the plugin registration step for the Update message, and access the data via the context.PostEntityImages collection


    Microsoft CRM MVP - http://mscrmuk.blogspot.com/ http://www.excitation.co.uk

    Monday, October 27, 2014 9:58 AM
    Moderator