Search code examples
javascriptpluginsdynamics-crm-2011ribbon

Disable/Enable Ribbon for set time


I have a plugin for Contact entity that triggers on Create. It then calls the WCF service and creates a new record in the back office (MSSQL). The same entity have another plugin that fires when record is Updated.

Issue I am having is when if I create a new record and within a minute change detail and try to save it, the update plugin fails. This is because the Create plugin is still running. (And the record I am trying to update still doesn't exist in the back office DB)

Is there a way to disable 'Save' or 'Save & Close' button in the ribbon while the status of the Create plugin is not Complete or Failed? Or at least disable it for say 120 Sec so it is not clicked on right away?

CRM 2011 RU18 On Premise. (All end users are restricted to use IE so I can use JS)

Thank you in advance,


Solution

  • My solution to this would be have some JavaScript waiting on the form and not allowing it to save until your plugin has passed in a "complete" message in which case the form can be saved. Near the end of your plugin code i would make it update a value on your form which the JavaScript would look at, if this field isn't populated, don't allow the form to save.

    The JavaScript is the easy part using the onSave method and making sure you pass the execution object as the first parameter and assuming you create the field for your plugin a bool

    function preventSave(exobj)
    {
      var pluginFinished = Xrm.Page.getAttribute("pluginFinished");
    
      if (pluginFinished.getValue() == false || pluginFinished.getValue() == null)
      {
        exobj.getEventArgs().preventDefault();
      }
    }
    

    if there isn't a value or null the JavaScript will not allow the form to save. You may want to provide a message after the preventDefault (prevent save) to alert the uses no save has occurred.

    Hope this helps