Question

We are building some plugins in Microsoft Dynamics CRM by inheriting from IPlugin. We have these configured so they fire whenever an Account is updated.

The problem is the plugins are calling our services, which causes our service to respond with an update. We are doing some pretty hacky things right now to prevent these cyclical updates from happening.

We were wondering if there was a way to pass a value to the IOrganizationService service (the web service) that a plugin can look at. Our other system could send a flag ("hey, don't bothing sending an update!") and the plugin could skip calling back.

Can we pass parameters from web service to the plugins?

Was it helpful?

Solution 2

We decided the correct solution was to use the value found in IPluginExecutionContext.InputParameters["Target"]. In the case of an Update, this returns an Entity containing attributes for all the attributes that were updated.

We basically have a list of attribute names we cared about. We loop through names and see if any of them appear in the entity attribute list. If so, we send an update to our other system. The good news is, Dynamics CRM ignores updates where the values don't actually change, so trying to update a value to itself is no-op.

public void Execute(IServiceProvider serviceProvider)
{
    IPluginExecutionContext context = serviceProvider.GetService(typeof(IPluginExecutionContext));
    Entity entity = (Entity)context.InputParameters["Target"];
    string[] fields = new string[] { "name", "statecode", "address1_line1" };
    bool hasUpdates = fields.Where(f => entity.Attributes.Contains(f)).Any();
    if (!hasUpdates)
    {
        return;
    }
}

OTHER TIPS

Good idea could be usage of custom flag-field. For example you add bit field and call it CallFromExternalSystem. So when you make an update from your external system through IOranizationService you just fill this flag with true field and in plugin you can check condition that this field is present in fields list so you have no need to call external system endpoint again.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top