locked
Update Plugin C# RRS feed

  • Question

  • Hi , 
    i need to retrieve a field value of a record and make some update to that same record. 

    I have registered my plugin in Update message , Post-Operation  , Image = Target

    I am able to retrieve value of the record . but i am not able to set value on the same record. 

    This is my bellow code 

      IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
    
                    IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                    IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
    
                    //Entity Candidate = (Entity)context.InputParameters["Target"];
    
                    if (context.MessageName == "Update")
                    {
    
                        if (context.PostEntityImages.Contains("Target"))
                        {
                            Entity Account = (Entity)context.PostEntityImages["Target"];
    
                            if (Account.Attributes.Contains("name"))
                            {
    
                             string  FullName = Account.GetAttributeValue<string>("name");
    
    
    
     Account["MobileNo"] = "2258717";
    }
    }


    Grateful for any help!!
    • Edited by kMAT1 Monday, September 30, 2013 6:04 AM
    Monday, September 30, 2013 6:00 AM

Answers

  • You can call 

    service.Update(entity);

    but you need to use IExecutionContext.Depth to avoid getting into infinite loop. 

    if(IExecutionContext.Depth <= 2)

    {

    //Update your record

    }

    Every time a running plug-in or Workflow issues a message request to the Web services that triggers another plug-in or Workflow to execute, the Depth property of the execution context is increased. If the depth property increments to its maximum value within the configured time limit, the platform considers this behavior an infinite loop and further plug-in or Workflow execution is aborted. The maximum depth (8) and time limit (one hour) are configurable by the Microsoft Dynamics CRM administrator.


    Here is how to use IExecutionContext.Depth in detail: Execution Context Depth

    It applies to both CRM 4.0 and 2011 version.



    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful" Thanks, Imran Abbasi

    • Proposed as answer by Imran I Abbasi Monday, September 30, 2013 6:15 AM
    • Marked as answer by kMAT1 Monday, September 30, 2013 6:25 AM
    Monday, September 30, 2013 6:15 AM