locked
Simple Plugin To Update a Field Issue RRS feed

  • Question

  • Hi,

    I've created a plugin that will add a test message to a field when my custom entity record (grm_contract) is updated.

    I have the below code registered as a Post Update operation in Synchronous mode. I receive no errors, the plugin completes and you can see the step "GRMContractPlugin: Updated grm_test" in the trace logs.

    The problem is that my field (grm_test) never gets updated. In reality I'm trying to do something slightly different but I've simplified my code down to the following (trying to rule other issues out.)

    using System;
    using System.ServiceModel;
    using System.Runtime.Serialization;
    using Microsoft.Xrm.Sdk;
    
    namespace GRMPlugins
    {
        public class OnContractUpdate : IPlugin
        {
    
            public void Execute(IServiceProvider serviceProvider)
            {
    
                //Extract the tracing service for use in debugging sandboxed plug-ins.
                ITracingService tracingService =
                    (ITracingService)serviceProvider.GetService(typeof(ITracingService));
    
                // Obtain the execution context from the service provider.
                IPluginExecutionContext context = (IPluginExecutionContext)
                    serviceProvider.GetService(typeof(IPluginExecutionContext));
    
                // The InputParameters collection contains all the data passed in the message request.
                if (context.InputParameters.Contains("Target") &&
                    context.InputParameters["Target"] is Entity)
                {
    
                    // Obtain the target entity from the Input Parameters.
                    tracingService.Trace
                        ("GRMContractPlugin: Getting the target entity from Input Parameters.");
                    Entity entity = (Entity)context.InputParameters["Target"];
    
                    // Obtain the image entity from the Pre Entity Images.
                    tracingService.Trace
                        ("GRMContractPlugin: Getting image entity from PreEntityImages.");
                    Entity image = (Entity)context.PreEntityImages["Target"];
    
                    // Verify that the target entity represents a grm_contract.
                    // If not, this plug-in was not registered correctly.
                    tracingService.Trace
                        ("GRMContractPlugin: Verifying that the target entity represents a grm_contract.");
                    if (entity.LogicalName != "grm_contract")
                        return;
    
                    entity.Attributes.Add("grm_test", "TESTING 123");
                    tracingService.Trace
                       ("GRMContractPlugin: Updated grm_test.");
    
    
    
                }
    
            }
        }
    
    }
    


    I can only assume this is a problem with the line:

    entity.Attributes.Add("grm_test", "TESTING 123");

    Does anybody have any ideas on why grm_test is not updating but at the same time is not throwing any errors?

    Thanks,

    James

    Thursday, October 15, 2015 3:35 PM

Answers

  • Hello James,

    Your code would work only in case your step is registered to handle in Pre-Validation or Pre-Operation mode and would not for Post-Operation. To make your code more readable you can use following code for assigning:

    entity["grm_test"] = "Testing 123";


    Dynamics CRM MVP
    My blog

    Thursday, October 15, 2015 4:13 PM
    Moderator

All replies

  • Hello James,

    Your code would work only in case your step is registered to handle in Pre-Validation or Pre-Operation mode and would not for Post-Operation. To make your code more readable you can use following code for assigning:

    entity["grm_test"] = "Testing 123";


    Dynamics CRM MVP
    My blog

    Thursday, October 15, 2015 4:13 PM
    Moderator
  • in post update Plug in, you need to call Service.Update method to update your changes.

    only on pre-validation and pre-operation mode you assign target attribute and system will update them, for post operation you need call update method passing entity which has been changed..

    // Get a reference to the organization service.
                IOrganizationServiceFactory factory = 
                    (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                IOrganizationService service = factory.CreateOrganizationService(context.UserId);
    
     //update entity
    entity["grm_test"] = "Testing 123";
    
    //make sure entity id specfied and then call update method
    
    service.Update(entity);
    


    MayankP
    My Blog
    Follow Me on Twitter

    Thursday, October 15, 2015 4:42 PM
  • Wouldn't this result in an infinite loop? Using service.Update(entity) on an Update plugin? Is there a way to stop this?
    Friday, October 16, 2015 6:09 AM
  • Hi Andrii, thanks again for this one! I've changed it to pre operation and it works fine as you suggested. I should be able to figure out the rest of the plugin now.
    Friday, October 16, 2015 6:19 AM
  • Wouldn't this result in an infinite loop? Using service.Update(entity) on an Update plugin? Is there a way to stop this?

    Hello,

    Yes, that could cause infinite loop. To check the level of call depth you can use config.Depth parameter.


    Dynamics CRM MVP
    My blog

    Friday, October 16, 2015 6:38 AM
    Moderator