locked
CRM 2011 plug-in error, the object cannot be updated because it is read-only RRS feed

  • Question

  • We have plug-in that fires on Update of a salesorder.   When you Cancel a sales order (using OOTB button on Ribbon in 2011 online), the plug-in fires again and is attempting to update a field that is set to read-only, due to the Cancel Order being clicked.  How can I check the state to prevent this error in my plug-in?  I have tried looking at the statecode value on my Entity in my plug-in code, but it says its null during debugging.  I also have looked at Context or statecode to see if I can maybe Exit plugin when set to readonly before it hits the "update code" that causes the original issue. The problem is at the point my plug-in fires, the record is not actually inactive, so I am not sure how I can check its state, so that I do not update fields and get the error of "object cannot be updated because it is read-only."  Does anyone know how to do this?  I would rather handle this in my plug-in code than JS, if possible. thank you.



    jm

    Thursday, October 17, 2013 7:30 PM

Answers

  • Hi,

    Have you registered your plugin at post-operation stage (after database operations are already executed). Your scenario requires that you should register it to work in Pre-Operation stage (before database operations are executed and before changes to the record are made to the database).

    In Pre-Operation stage, you will have the updated field values just before CRM writes them to database and record is effectively still active at this stage. Record would be made inactive in 'MainOperation' stage of plugin which comes after 'Pre-Operation' stage and when Pre-Operation stage is complete. You can cancel the update of record at 'Pre-Operation' stage and roll-back the transaction.


    You can get the details of plugin stages at: http://msdn.microsoft.com/en-us/library/gg327941.aspx


    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful" Thanks, Imran Abbasi

    • Proposed as answer by Imran I Abbasi Thursday, October 17, 2013 10:50 PM
    • Marked as answer by dfgs435 Friday, October 18, 2013 3:17 PM
    Thursday, October 17, 2013 10:50 PM

All replies

  • Hi,

    Have you registered your plugin at post-operation stage (after database operations are already executed). Your scenario requires that you should register it to work in Pre-Operation stage (before database operations are executed and before changes to the record are made to the database).

    In Pre-Operation stage, you will have the updated field values just before CRM writes them to database and record is effectively still active at this stage. Record would be made inactive in 'MainOperation' stage of plugin which comes after 'Pre-Operation' stage and when Pre-Operation stage is complete. You can cancel the update of record at 'Pre-Operation' stage and roll-back the transaction.


    You can get the details of plugin stages at: http://msdn.microsoft.com/en-us/library/gg327941.aspx


    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful" Thanks, Imran Abbasi

    • Proposed as answer by Imran I Abbasi Thursday, October 17, 2013 10:50 PM
    • Marked as answer by dfgs435 Friday, October 18, 2013 3:17 PM
    Thursday, October 17, 2013 10:50 PM
  • You are the man Imran, and saved me from writing crazy statecode logic...

    jm

    Friday, October 18, 2013 3:18 PM
  • No worries my dear, happy to help :-)

    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful" Thanks, Imran Abbasi

    Saturday, October 19, 2013 11:33 AM
  • You can set state to change the state of Sales Order and update whatever fields what you want and again fulfill the same order.

    Remember to use  Late binding using svcutil.exe



    Wednesday, October 7, 2015 9:46 AM