Anil P
Anil P

Reputation: 163

CRM Plugin Call on Field Update call WCF

I want to trigger a plugin when particular field is being updated.

Here is the code which I am calling

public class CaseStageUpdate : IPlugin
{
    public void Execute(IServiceProvider serviceProvider)
    {
        try
        {
            // Extract the tracing service for use in debugging sandboxed plug-ins.
            ITracingService tracingService =
                (ITracingService)serviceProvider.GetService(typeof(ITracingService));

            if (serviceProvider == null)
            {
                throw new ArgumentNullException("serviceProvider");
            }

            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

            Entity entity;
            if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
            {
                string strParams = "None";
                entity = (Entity)context.InputParameters["Target"];
                if (entity.LogicalName == "entityname")
                {
                    // Create Matter Process
                    try
                    {

                        IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                        IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

                        try
                        {

                            if (entity.Attributes.ContainsKey("field")){// call web service}

                        }
                        catch (Exception ex)
                        {
                            tracingService.Trace("Plugin Exception : \n {0}", ex.ToString());
                            throw;
                        }

                    }
                    catch (Exception ex)
                    {
                        tracingService.Trace("Plugin Exception : \n {0}", ex.ToString());
                        throw;
                    }
                }
            }
            else
            {
                throw new InvalidPluginExecutionException("Plugin is not valid for this enity.");
            }
        }
        catch (Exception ex)
        {

        }
    }
}

In the WCF I am updating entity, but plugin called again because the updated field again found in the plugin. But I haven't update the field.

Suppose I want to update ABC field on AAA field update. I trigger to set when AAA field is found I will update ABC field.

WCF Code

Entity entity = service.Retrieve("entityname", Guid.Parse(Id), new ColumnSet(true));
entity.Attributes["ABC"] = "TEST";
service.Update(entity);  

Upvotes: 0

Views: 505

Answers (1)

Daryl
Daryl

Reputation: 18895

You should be using the Target to update an existing field on the existing entity, not retrieving the entity to update the field.

// 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.
    var entity = (Entity)context.InputParameters["Target"];
    entity.Attributes["ABC"] = "TEST";
    // No Need to call Update on the target.  It'll get updated as a part of the Plugin Process
}

Also, Remove all but one sets of your Try's:

public void Execute(IServiceProvider serviceProvider)
{
    try
    {
        // Extract the tracing service for use in debugging sandboxed plug-ins.
        ITracingService tracingService =
            (ITracingService)serviceProvider.GetService(typeof(ITracingService));

        if (serviceProvider == null)
        {
            throw new ArgumentNullException("serviceProvider");
        }

        IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

        Entity entity;
        if (!context.InputParameters.Contains("Target") || !(context.InputParameters["Target"] is Entity))
        {
            throw new InvalidPluginExecutionException("Plugin is not valid for this enity.");
        }

        entity = (Entity)context.InputParameters["Target"];
        if (entity.LogicalName == "entityname")
        {
            // Create Matter Process
            IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

            if (entity.Attributes.ContainsKey("field")){// call web service}
        }
    }
    catch (Exception ex)
    {
        tracingService.Trace("Plugin Exception : \n {0}", ex.ToString());
        throw;
    }
}

Upvotes: 1

Related Questions