locked
Update Pre Operation plugin RRS feed

  • Question

  • Hello CRM friends,

    I have a question. I am trying to update a lookup field value just before the record state changes to 'Canceled'. I am thinking of registering the plugin on update message and preoperation.
    In my code I am checking if the statecode has changed since target entity contains only the fields that are changed. But my confusion is I don't want to trigger plugin each time the statecode is changed.
    I want to trigger it only when statecode is changed to 'Canceled'. How can I do that? Also, is my updating the lookup field correct?

      protected void ExecutePreCallsMerge(LocalPluginContext localContext)
            {
                if (localContext == null)
                {
                    throw new ArgumentNullException("localContext");
                }
    
                IPluginExecutionContext context = localContext.PluginExecutionContext;
                IOrganizationService service = localContext.OrganizationService;
    
                if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
                {
                    Entity entity = (Entity)context.InputParameters["Target"];
                    if (entity.LogicalName == "task")
                    {
                        try
                        {
                            if (entity.Attributes.Contains("statecode"))
                                {
                                   
                                    entity["new_workby"] = new EntityReference("new_new_workby_task_workby", entity.Id);
                                    //adding attribute using the add function
                                    entity.Attributes.Add("new_workby", "None");
    
                                    service.Update(entity);
                                }
                        }
                        catch (FaultException ex)
                        {
                            throw new InvalidPluginExecutionException("An error occurred in the plug-in.", ex);
                        }
                    }
                }
            }

    Thanks

    Friday, November 21, 2014 3:27 PM

All replies

  • Hi,

    Just Read the option Set value and return if it is not cancelled. I am not clear with your requirement. Why are you updating task record itself? new_workby is lookup to which entity? When we want to update the entity on which plugin fired and plugin is on preoperation you can just update input property bag as given below.

    protected void ExecutePreCallsMerge(LocalPluginContext localContext)
            {
                if (localContext == null)
                {
                    throw new ArgumentNullException("localContext");
                }

                IPluginExecutionContext context = localContext.PluginExecutionContext;
                IOrganizationService service = localContext.OrganizationService;

                if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
                {
                    Entity entity = (Entity)context.InputParameters["Target"];
                    if (entity.LogicalName == "task")
                    {
                        try
                        {
                            if (entity.Attributes.Contains("statecode"))
                                {
                                   int OptionsetValue = ((OptionSetValue)entity["statecode"]).Value;
                                   if(OptionsetValue==2)
                                     {
                                         // how can you set the task as lookup to self. I assume you want to set User Id.
                                          entity["new_workby"] = new EntityReference("Specify Look Up field Name", context.UserId);
                                     

                                         
                                    }
                                }
                        }
                        catch (FaultException ex)
                        {
                            throw new InvalidPluginExecutionException("An error occurred in the plug-in.", ex);
                        }
                    }
                }
            }

    Thanks!

    Friday, November 21, 2014 6:04 PM
  • Sorry my bad. That is a mistake before posting the code in here.

    I am trying to update a lookup field called 'new_taskphase' to 'Complete' before the record statecode changes to 'Canceled'.

    Following your suggestion I've made changes to code but it is still not working.

    COuld you please take a look at it?

     protected void ExecutePreTaskUpdate(LocalPluginContext localContext)
            {
                if (localContext == null)
                {
                    throw new ArgumentNullException("localContext");
                }
    
                // TODO: Implement your custom Plug-in business logic.
                IPluginExecutionContext context = localContext.PluginExecutionContext;
                IOrganizationService service = localContext.OrganizationService;
                if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
                {
                    Entity entity = (Entity)context.InputParameters["Target"];
                    if (entity.LogicalName == "task")
                    {
                        try
                        {
                                if (entity.Attributes.Contains("statecode"))
                                {
                                    int OptionsetValue = ((OptionSetValue)entity["statecode"]).Value;
                                    if (OptionsetValue == 2)
                                    {
                                        entity["new_taskphase"] = new EntityReference("new_taskphase", entity.Id);
                                        entity.Attributes.Add("new_taskphase", "Complete");
                                        localContext.OrganizationService.Update(entity);
                                    }
                                }
                        }
                        catch (FaultException ex)
                        {
                            throw new InvalidPluginExecutionException("An error occurred in the plug-in.", ex);
                        }
                    }
                }
            }
    Thanks

    Friday, November 21, 2014 8:11 PM
  • Hi,

    Can you please confirm field type of new_teaskphase? If it is an option set you need to specify the Optionset value. No need to update in case of pre operation.


               
    protected void ExecutePreTaskUpdate(LocalPluginContext localContext)
            {
                if (localContext == null)
                {
                    throw new ArgumentNullException("localContext");
                }
    
                // TODO: Implement your custom Plug-in business logic.
                IPluginExecutionContext context = localContext.PluginExecutionContext;
                IOrganizationService service = localContext.OrganizationService;
                if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
                {
                    Entity entity = (Entity)context.InputParameters["Target"];
                    if (entity.LogicalName == "task")
                    {
                        try
                        {
                                if (entity.Attributes.Contains("statecode"))
                                {
                                    int OptionsetValue = ((OptionSetValue)entity["statecode"]).Value;
                                    if (OptionsetValue == 2)
                                    {
                                         //  Specify the value of Option set field. Dont need label here.
                                           entity.Attributes["new_taskphase"] = new OptionSetValue( Integer Option set value); } } } catch (FaultException ex) { throw new InvalidPluginExecutionException("An error occurred in the plug-in.", ex); } } } }

    Thanks!

    Friday, November 21, 2014 8:34 PM
  • new_taskphase is a lookup field.

    Friday, November 21, 2014 8:44 PM
  • Hi,

    You can specify the value as given below.

    entity["new_taskphase"] = new EntityReference("new_taskphase", Here specify the Guid of the Complete Task phase record.);

    Thanks!

     
    • Proposed as answer by Kalim Khan Monday, November 24, 2014 6:31 PM
    Friday, November 21, 2014 8:49 PM
  • Also, if it's a pre plugin you don't need to call service.update()

    Ibrahim Sukari, Technical Consultant | SharePoint | Dynamics CRM | LinkedIn Profile

    Sunday, November 23, 2014 12:37 PM
  • Already mentioned in my post.. No need to update in case of pre operation.

    Thanks!

    Tuesday, November 25, 2014 11:24 AM