0
votes

I wrote two plugins for CRM Dynamics 2011. One is configured to be triggered during the creation of an Opportunity Product (Step added with Message set to "Create"). The other is configured to be triggered during the update of an Opportunity Product (Step added with Message set to "Update"). Both plugins write CRM data to an external SQL database (insert in one case, update in the other).

The first plugin alone works great (Pre operation). It's only called during opportunity product creation and the data is successfully written to my external database.

Once I activate the second plugin (Post operation + pre image), if I edit an opportunity product, it works great too. However, the plugins is also called when I create a new one... I really don't understand where this is coming from as I made sure of the following: - The two plugins are completely separated. I have two different assembly with a single step in each. - In each plugin, I have a condition checking the context.MessageName value (Create or Update) before the code is ran.

Do you guys have an idea where this is coming from ?

EDIT 1 (18/12/2013): I believe the Update plugin is called upon creation because of a "On Save" workflow that actually update some fields in the opportunity product. I am aiming toward using SharedVariables to prevent the execution of the Update plugin if the Create plugin is ran first. Here is the code used for sharing a variable between the two plugins. The SharedVariables.Contains check in the Update plugin is always false. To test this I create a brand new Opportunity Product while debugging the Update plugin.

Create Plugin

Dim setFlag As Boolean = True
  context.SharedVariables.Add("NewSFR", setFlag)

Update Plugin

If context.SharedVariables.Contains("NewSFR") Then
  Dim receivedFlag As Boolean = context.SharedVariables("NewSFR")
  If receivedFlag Then
    Return
  End If
End If
4
Debug the plugin to see why is not doing the check of context.MessageName correctly. - Jorge
I did: when I create a new opportunity product, for some reason both the Create and Update plugins are ran. Which is weird as I clearly registered my Update plugin with a Step set to "Update". As the step is triggered, the context.MessageName is equal to "update" and the condition is met. - Clement
The problem is that the plugin should never be triggered when editing an opportunity product. Or I'm missing something obvious.. - Clement

4 Answers

1
votes

I found a solution to work around this.

I created a custom field in the opportunityproduct table to be used as a flag. Then using Javascript on the OnLoad oppportunityproduct form, I set the field to 0 or 1 regarding what type of form is loaded (create or update). The javascript code to do so is:

var FormType = Xrm.Page.ui.getFormType();

If FormType = 1, then it's a Create Form Type and I set my flag to 1. If FormType = 2, then it's an Update Form Type and I set my flag to 0.

Then I check the flag value in my Update plugin and exit if equal to 1.

0
votes

This commonly happens if you attempt to call a service.Update() on the entity, or if a workflow is updating a field

0
votes

If indeed there’s no other plug-in calling update then this might be by design i.e. crm is updating the entity to include it in its calculations.

To work around this you can:

  1. Check if the Context.Depth is correct if it’s not 1 you can wrap your update with proper condition

  2. Add a SharedVariable in the pre Create and check if it’s there in the post Update. e.g.

    context.SharedVariables.Add("key", (Object)value); // create shared var
    

    You can read more about SharedVariable here http://msdn.microsoft.com/en-us/library/gg328579.aspx

  3. Finally, since you’re in pre Create you might include a flag attribute in the Target and check if the Target contains this flag In the post event.

0
votes

This is a bug in Dynamics CRM where the Update plugin is triggered on Create. I have faced this as well in all the implementations that I have done. The way around is providing a checking inside the Plugin code that if the MessageName is 'Update', and then provide entry to the actual code.