locked
How to differenciate between the Update messate that gets triggered from Deactivate button vs the one from Save button on a custom field! RRS feed

  • Question

  • Hi,

    I have a custom entity which has a custom field (lets call it MyField).

    I want the following behavior:

    • If user clicks on Save and MyField has value in it, I want a plugin to throw an exception and ask the user to clear this field before saving the record.
    • If user clicks on Deactivate and MyField does not have value, I want the plugin to throw an exception and ask the user to put some value in the field before deactivating. 

    Sounds pretty easy, but I noticed that deactivate button triggers two updates in the following order:

    1. Update to save all fields on the form (has MyField in the context but does not have statecode in the context when plugin gets fired)
    2. Update to set state (only has system fields and statecode and does not have MyField in the context when plugin gets fired)

    The problem I am having is that in my plugin (that is registered on Pre-Updae) there is no way to differentiate between the update 1 that gets triggered from Deactivate Button vs the one that gets triggered from Save button and I need that differentiation.

    Any ideas? If this is not clear enough please let me know to elaborate more.


    captainsina


    • Edited by captainsina Thursday, December 5, 2013 8:47 PM
    Thursday, December 5, 2013 8:22 PM

Answers

All replies

  • Check this thread

    http://social.microsoft.com/Forums/en-US/061de446-0f4b-4a30-b1e1-04676a7db19b/crm-2011-detect-activatedeactivate-operations-on-update-plugins?forum=crmdevelopment


    If my response helps you in finding your answer then please click 'Mark as Answer' and 'Vote as Helpful'

    Thursday, December 5, 2013 11:42 PM
  • Hello,

    I would suggest you to use Client Side checks before plugins. You can check on client side what is Save Mode (Change State or simple Update). Recheck following article - http://msdn.microsoft.com/en-us/library/bf64f471-7582-45e7-b8ed-370bfbecda68#BKMK_GetSaveMode

    After that you can develop plugins. In case your client side check will  block save - plugin even would not be triggered.


    Dynamics CRM MVP/ Technical Evangelist at SlickData LLC
    My blog
    Friday, December 6, 2013 1:00 AM
    Moderator
  • Thanks for sharing this, but this cant help me in my case reason being the first update that gets fired by Deactivate is exactly like any other update, the message in its parent context is update as well. So i cannot differentiate between this update and the one that gets triggered if user actually clicks on Save button and I need that differentiation.

    captainsina

    Friday, December 6, 2013 2:10 PM
  • What attributes have you registered your Update against?

    Try filtering the update message to just be triggered by MyField and nothing else.

    You will also need to use a Pre Entity Image which will also include MyField. If MyField is not present in the entity.Attributes collection then you can check if it is present in the pre-entity image too and if so throw an exception.

    Then register the plugin on SetState and SetStateDynamicEntity to pick the De-activate request pre-operation. You can then check for the presence of MyField again and this time if it isn't throw an exception.
    • Proposed as answer by SteveGreen Friday, December 6, 2013 5:08 PM
    • Unproposed as answer by SteveGreen Friday, December 6, 2013 5:08 PM
    • Edited by SteveGreen Friday, December 6, 2013 5:11 PM
    • Proposed as answer by SteveGreen Friday, December 6, 2013 5:11 PM
    Friday, December 6, 2013 5:08 PM
  • Hello,

    I would suggest you to use Client Side checks before plugins. You can check on client side what is Save Mode (Change State or simple Update). Recheck following article - http://msdn.microsoft.com/en-us/library/bf64f471-7582-45e7-b8ed-370bfbecda68#BKMK_GetSaveMode

    After that you can develop plugins. In case your client side check will  block save - plugin even would not be triggered.


    Dynamics CRM MVP/ Technical Evangelist at SlickData LLC
    My blog

    I knew that this can be done if we do some work on client side, but was wondering if it is possible to differenciate between those updates on server side or not and looks like it is not possible.

    Cheers.


    captainsina

    Friday, December 6, 2013 9:29 PM