locked
Updating a lookup field plugin RRS feed

  • Question

  • Hi,

    This plugin is to update a lookup field on Merge message and pre-operation. Nothing is happening when cases are merged using merge button. Can someone please take a look at my code. Thank you.

     protected void ExecutePreCallsMerge(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;
    
                // 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 parmameters.
                    Entity entity = (Entity)context.InputParameters["Target"];
    
                    try
                    {
                        Guid subordinateId = (Guid)context.InputParameters["SubordinateId"];
    
                        var fetch = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
                                      <entity name='activity'>
                                        <attribute name='new_status' />
                                        <filter type='and'>
                                          <filter type='and'>
                                            <condition attribute='regardingobjectid' operator='eq' uitype='incident' value='" + subordinateId + @"' />
                                            <condition attribute='statecode' operator='eq' value='0' />
                                          </filter>
                                        </filter>
                                      </entity>
                                    </fetch>";
    
                        EntityCollection ec = service.RetrieveMultiple(new FetchExpression(fetch));
                        if (ec.Entities.Count > 0)
                        {
                            // Create an ExecuteMultipleRequest object.
                            ExecuteMultipleRequest multipleRequest = new ExecuteMultipleRequest()
                            {
                                // Assign settings that define execution behavior: continue on error, return responses. 
                                Settings = new ExecuteMultipleSettings()
                                {
                                    ContinueOnError = false,
                                    ReturnResponses = true
                                },
                                // Create an empty organization request collection.
                                Requests = new OrganizationRequestCollection()
                            };
    
                            foreach (var item in ec.Entities)
                            {
                                item.Attributes["new_status"] = "Closed";
                                UpdateRequest updateRequest = new UpdateRequest { Target = item };
                                multipleRequest.Requests.Add(updateRequest);
                            }
    
                            // Execute all the requests in the request collection using a single web method call.
                            ExecuteMultipleResponse multipleResponse = (ExecuteMultipleResponse)service.Execute(multipleRequest);
                        }
                    }
                    catch (Exception Ex)
                    {
                        throw new InvalidPluginExecutionException(Ex.ToString());
                    }
                }
    
            }

    Thursday, November 13, 2014 7:04 PM