Duplicate Assigns On Create

(This is a repost of the content which was previously available on the IW community site). Here is an interesting effect which I solved recently: when an item in Microsoft Dynamics CRM (MSCRM) was created, the assign event (both callout and workflow) would fire twice. This was very odd, and great lengths were taken to track it down—using the MSCRM trace tool, various tests, etc. But since this was a one-off issue that could not be replicated at other sites, it was eventually shelved until more important matters were resolved.

This issue only occurred during creations from external sources (namely, my code) and did not happen if the item was created in the MSCRM interface. However, the code itself was standard—I created the item, set its properties, and passed it to the Create method. Nothing fancy there.

The root cause? I couldn’t see the forest for the trees: I was setting the ownerid parameter as one of the properties. Internally, this was causing the assign event to fire when the item was created and triggering a second assign to handle the ownerid. Removing that single line of code fixed the issue immediately.