locked
Prevent workflow triggered by a plugin in CRM 2011 RRS feed

  • Question

  • Hi.

    I have two 1:N relationship entities in CRM 2011.  Entity A has two fields: field A1, field A2; Entity B has two fields: field B1, field B2.  The requirement is field A1 and field B1 must be synced, the same as field A2 and field B2.

    I used a workflow to save field B1 to field A1, field B2  to field A2; and a plugin to save field A1  to field B1, field A2  to field B2;.

    The problem is plugin and workflow trigger each other and create a infinite loop.

    I can't prevent the plugin from being triggered by a workflow, but I can used 'if (context.Depth > 1)' in plugin to stop it from running again after it is triggered a workflow, because context.Depth = 3, when the plugin context enters Entity A from a workflow.

    Now, my question is, in the workflow, how can I prevent it from being triggered by a plugin?

    Thanks for any answer or discussion.

    Tuesday, August 19, 2014 3:37 PM

All replies

  • Just curious, why can't they both be on a plugin so that you could check the depth on each?
    Tuesday, August 19, 2014 6:30 PM
  • Instead of just checking for context.Depth you need to also check for CallerOrigin.

    When the form triggers the plug-in update the CallerOrigin is ApplicationOrigin and when the workflow triggers the plug-in update it’s AsyncServiceOrigin

    You can read more about the caller origin here https://community.dynamics.com/crm/b/hardworkdays/archive/2012/06/01/ms-crm-2011-callerorigin-in-plugins.aspx

    And utilize that in your PI code.

    Another option is to create a field that identifies the WF update. The PI can check the field existence in the Target and stop the PI update. This field can only be updated using the WF.

    Cheers


    Dev Blog: Dynamics CRM - Thinking outside the Box

    Tuesday, August 19, 2014 8:22 PM
  • Thanks both of you.

    To answer JBlaeske: it is good point to use Plugin in both directions.  When workflow is available from child entity to parent entity, it is just a lot easier.

    Hi, Adi: I got it in plugin.  But the question is in workflow - how to prevent workflow to be triggered by plugin.  Thanks in advance.

    Wednesday, August 20, 2014 12:49 PM
  • You can’t do that in the WF alone. You need to use the PI code to identify that the WF is triggering the PI (using caller origin) and avoid the loop.


    Dev Blog: Dynamics CRM - Thinking outside the Box

    Wednesday, August 20, 2014 3:57 PM