ian486
ian486

Reputation: 11

CRM 2011 Multiple Plugins firing at same time, retrieving invalid data

I am working on Dynamics CRM 2011,I have created a Order Product Create Plugin(Post Operation) and also an Order Product Delete Plugin (Pre Validation).

When an Order Product is created, my plugin retrieves a parent record and updates a quantity field according to the Order Product Quantity. When an Order product is deleted, my delete plugin reverses this and adds back a quantity to a parent record.

My problem is that I have a custom HTML Resource that calls an OData Post that creates Order Products in Batches (simulating a Bulk Create), my Script calls this Creation in a loop. For instance, I may call the OData Create 5 times in a row to quickly create 5 custom Order Products, Or I may call it 10 times, depending on the users desire. It looks like my plugin is firing at the same time as the value that is retrieved from the parent record sometimes is the same instead of an updated value. My intention is that each plugin fire and update the parent record before the next plugin fires/retrieves the quantity value. If I create 5 order products with a quantity of 1 each, I expect my parent record to decrement by 5. In reality it only decrements by 1 or 2 in a 5 Order Product Create situation. It looks like the retrieve org service calls in each plugin must be firing at the same time to grab the old value.

On the other hand, my delete plugin works perfectly in a Bulk Delete situation. I can delete 5 Order Products in a Bulk Delete and the Parent Record is updated correctly. For instance 5 Order products with quantity of 1 each, results in parent record updating by 5.

Why would a bulk delete work differently then me calling a Odata Post a few times. Do you think that moving this from a Plugin to a Workflow process would be a better solution?

Thank you Ian

Upvotes: 0

Views: 1009

Answers (2)

Dot_NET Pro
Dot_NET Pro

Reputation: 2123

You should use Plugin Execution Order to execute plugins in order you want.

Execution order in plugin specifies the order, also known as rank, that plug-ins are executed within a pipeline stage. Plug-ins registered with an order value of 1 are executed first, followed by plug-ins registered with an order of 2, and so on. However, if there is more than one plug-in in a stage with the same order value, then the plug-in with the earliest compilation date is called first

See Image Below

enter image description here

Upvotes: 1

Alessi
Alessi

Reputation: 769

You may be experiencing a race condition as a result of the plugin being triggered simultaneously after two or more Order Products are created at the same time for the same Product.

e.g. Product Lemon has 10 stocked items. Order Product A orders 2 Lemons. Order Product B orders 3 Lemons.

If Product Order A and B triggers your plugin at the same time, they will both grab the current stock count for Product Lemon which is 10.

Both plugin will deduct from 10 (i.e. Product Order A will be 10-2, while Product B will be 10-3). Depending on who gets to update the Product Lemon record last will be the new stock count for Product Lemon.

Solution: Use a MUTEX to prevent a race condition in the calculation. NB 1: MUTEX is not available in a CRM Online environment. NB 2: (lock) is supported in CRM Online, but not for Cross-Process locking.

Upvotes: 0

Related Questions