Search code examples
pluginsdynamics-crmcrmdynamics-crm-2013dynamics-crm-online

Update subgrid by parent in Dynamics CRM (opportunity)


I have opportunity, what I need is - when the est time changed the valid_to changed in the subgrid also changed to the same value. I have tried to write plugin to do that for me but nothing is happening, the product in the subgrid values still the same. What’s wrong?

I made plugin and this is the code:

  public void Execute(IServiceProvider serviceProvider)
    {
        // extract the service provider
        ITracingService tracingservice = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
        IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
        IOrganizationServiceFactory srevicefactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
        IOrganizationService service = srevicefactory.CreateOrganizationService(context.UserId);
        if(context.InputParameters.Contains("Target")&&context.InputParameters["Target"] is Entity)
        {
            Entity entity = (Entity)context.InputParameters["Target"];
            if (entity.Contains("name"))
             {
                 var fetch = @"<fetch no-lock='true' >

                         <entity name='opportunity' >

                           <attribute name='contactid'/>

                           <filter>

                             <condition attribute='opportunityid' operator='eq' value='{0}' />

                           </filter>

                         </entity>

                       </fetch>";
                 var fetchXML = string.Format(fetch, entity.Id);

                 var allContacts = service.RetrieveMultiple(new FetchExpression(fetchXML)).Entities;
                 foreach (var contactEnt in allContacts)
                 {

                     Entity contactToUpdate = new Entity("opportunityproduct", contactEnt.Id);

                     contactToUpdate["new_valid_to"] = entity["estimatedclosedate"];

                     service.Update(contactToUpdate);

                 }
             }
        }
    }

enter image description here


Solution

  • I recommend you few things, for a beginner to learn.

    1. Use tracingservice.Trace to trace the code execution and debug the issue
    2. You can use profiler or simply by throwing InvalidPluginExecutionException for troubleshooting

    This could be a simple copy/paste error. But the code is checking for attribute “name”, if there is no change made to name attribute in target entity, then this condition fails. Maybe it should be checking “estimatedclosedate” attribute if your plugin step filtering attribute is the same.

    if (entity.Contains("estimatedclosedate")) //changed name into estimatedclosedate
    {
        tracingService.Trace("condition passed and est_date is in target entity.");
        throw new InvalidPluginExecutionException("Debugging...");