locked
MS CRM 4.0 Plugin - why does my plugin update the wrong record? RRS feed

  • Question

  • Hi All - I have just spotted that my plugin seems to be updating the wrong record if the same plugin is triggered by two users at the same time. It's meant to update a value on another database (using a web serivce) if the name of the opportunity or account change. However, if two users update two different Opportunities, the record in the other database is updated with the name of the wrong Opportunity - it seems to set the value of the second opportunity to the value of the first opportunity!

    Is this a known issue? Or a known issue with using CRM plugins and web services? Shouldn't CRM be able to handle each instance of a plugin being triggered. Or do you think there is an issuewith variables? Should i be 'locking' the class while it is being run by a thread (dont even know if this is possible).

    I can provide code if needed.

    Thanks :-)

    Thursday, May 12, 2011 10:35 AM

Answers

  • Chances are that you are using instance variables in your plugin; this causes such problems, because different users triggering the plugin on different records (and even at different times) will not have different plugin instances.

    An instance of your plugin class is created when the plugin is first run and then always reused (until the plugin assembly is updated or the server restarted etc.).

    You should keep all data pertaining to specific plugin runs within the same thread, i.e. in local variables in the Execute() method, and if you need to call other methods, all information should be passed as parameters.

    • Proposed as answer by Jan AS Thursday, May 12, 2011 12:31 PM
    • Marked as answer by bshah1985 Friday, May 13, 2011 9:06 AM
    Thursday, May 12, 2011 11:39 AM

All replies

  • Chances are that you are using instance variables in your plugin; this causes such problems, because different users triggering the plugin on different records (and even at different times) will not have different plugin instances.

    An instance of your plugin class is created when the plugin is first run and then always reused (until the plugin assembly is updated or the server restarted etc.).

    You should keep all data pertaining to specific plugin runs within the same thread, i.e. in local variables in the Execute() method, and if you need to call other methods, all information should be passed as parameters.

    • Proposed as answer by Jan AS Thursday, May 12, 2011 12:31 PM
    • Marked as answer by bshah1985 Friday, May 13, 2011 9:06 AM
    Thursday, May 12, 2011 11:39 AM
  • Chances are that you are using instance variables in your plugin; this causes such problems, because different users triggering the plugin on different records (and even at different times) will not have different plugin instances.

    An instance of your plugin class is created when the plugin is first run and then always reused (until the plugin assembly is updated or the server restarted etc.).

    You should keep all data pertaining to specific plugin runs within the same thread, i.e. in local variables in the Execute() method, and if you need to call other methods, all information should be passed as parameters.

    Thank you! That worked!!! :-)
    Friday, May 13, 2011 9:06 AM