locked
CRM 2011 - JavaScript - check that Save form is complete? RRS feed

  • Question

  • Is it possible in JavaScript to check if a form has saved successfully before launching a dialog? I want to launch a dialog after the save event, but only after the Save event is complete.  The only thing I could think to do is create a setTimeout function, but it doesn't seem ideal.  Does anyone have any suggestions?  Thank you!
    Thursday, August 1, 2013 1:57 AM

Answers

  • Hi,

    The field can be a whole number which can be incremented by 1 every time you save the form.

    I'm referring to write a post-operation plugin as this stage is executed after the save has been committed to the database whereas JavaScript Save event is validated before the actual Save of the form. 

    I hope you have the knowledge of C#.NET and writing a simple plugin. (Even if you don't go ahead and write one as it will be required some or the other time)

    Here is the link to write a plugin: http://mscrmshop.blogspot.com.au/2010/10/crm-2011-plugin-tutorial.html and more information on plugins is given at: http://msdn.microsoft.com/en-us/library/gg328490.aspx

    CRM SDK link is http://www.microsoft.com/en-us/download/details.aspx?id=24004 which can be extracted and then you'll find Plugin Registration Tool within the bin folder of the extracted files. This tool can be used to register your plugin.

    Moreover, in the tutorial, instead of creating a task, you would just need to increment your field by 1 and then call service.Update

    Let me know incase you have any query.

    I hope it helps.


    Anubhav Bajpai

    • Marked as answer by wikky2007 Friday, August 2, 2013 1:17 PM
    Friday, August 2, 2013 5:25 AM
  • Thanks!  We ended up using JavaScript which sets a global variable in a separate JavaScript library.  In our OnLoad event, this variable is set to the value of the executionObj.getEventArgs().isDefaultPrevented() to determine whether the save is successful or not.  We also have a loop in the OnLoad event that sets a timer and checks to see if the form has completed loading:

        var loopMax = 100;
        var loopCtr;
        for (var i = 0; i < loopMax && globalVariables.FormState.FormLoadComplete == false; i++) {
            loopCtr = i;
            setTimeout(function () {; }, 100);
        }
    
        if (loopCtr == loopMax) {
            alert("Formload never finished");
        }


    Also, we are passing executionObj to our OnSave event as a parameter like this

    function Form_OnSave(executionObj)

    and have checked Pass execution context as first parameter in the CRM UI for our OnLoad event.

    OnSave event code:

    globalVariables.FormState.DefaultPrevented = executionObj.getEventArgs().isDefaultPrevented();

    • Marked as answer by wikky2007 Friday, August 2, 2013 1:39 PM
    Friday, August 2, 2013 1:39 PM

All replies

  • Hi,

    In my earlier development i had a similar requirement and what i did was is create a cookie variable on the 'OnSave' function and read the same variable on the 'OnLoad'. Because every time when you hit Save button the form get saved and get refreshed. Once you read the cookie variable you can launch your dialog window. Make sure you delete your cookie variable after you launch your Dialog window.

    Safi. 


    SAFI

    Thursday, August 1, 2013 2:52 AM
  • Hi,

    What I would suggest is to have a counter field on the form that is hidden and update it during the PostOperation of the update.

    Thereafter you can have your dialog triggered on that update field. I haven't tested it but you can definitely try this out.

    This way there will not be any security issue like cookies on client side.

    Thanks,


    Anubhav Bajpai

    Thursday, August 1, 2013 4:42 AM
  • Thanks for the responses.  Anubhav--should the field be a Whole Number then?  Also, what is PostOperation?  Please forgive me as my knowledge of JavaScript is somewhat limited.  How would I tell the counter field that the form is done saving?
    Thursday, August 1, 2013 5:14 PM
  • Hi,

    The field can be a whole number which can be incremented by 1 every time you save the form.

    I'm referring to write a post-operation plugin as this stage is executed after the save has been committed to the database whereas JavaScript Save event is validated before the actual Save of the form. 

    I hope you have the knowledge of C#.NET and writing a simple plugin. (Even if you don't go ahead and write one as it will be required some or the other time)

    Here is the link to write a plugin: http://mscrmshop.blogspot.com.au/2010/10/crm-2011-plugin-tutorial.html and more information on plugins is given at: http://msdn.microsoft.com/en-us/library/gg328490.aspx

    CRM SDK link is http://www.microsoft.com/en-us/download/details.aspx?id=24004 which can be extracted and then you'll find Plugin Registration Tool within the bin folder of the extracted files. This tool can be used to register your plugin.

    Moreover, in the tutorial, instead of creating a task, you would just need to increment your field by 1 and then call service.Update

    Let me know incase you have any query.

    I hope it helps.


    Anubhav Bajpai

    • Marked as answer by wikky2007 Friday, August 2, 2013 1:17 PM
    Friday, August 2, 2013 5:25 AM
  • Thanks!  We ended up using JavaScript which sets a global variable in a separate JavaScript library.  In our OnLoad event, this variable is set to the value of the executionObj.getEventArgs().isDefaultPrevented() to determine whether the save is successful or not.  We also have a loop in the OnLoad event that sets a timer and checks to see if the form has completed loading:

        var loopMax = 100;
        var loopCtr;
        for (var i = 0; i < loopMax && globalVariables.FormState.FormLoadComplete == false; i++) {
            loopCtr = i;
            setTimeout(function () {; }, 100);
        }
    
        if (loopCtr == loopMax) {
            alert("Formload never finished");
        }


    Also, we are passing executionObj to our OnSave event as a parameter like this

    function Form_OnSave(executionObj)

    and have checked Pass execution context as first parameter in the CRM UI for our OnLoad event.

    OnSave event code:

    globalVariables.FormState.DefaultPrevented = executionObj.getEventArgs().isDefaultPrevented();

    • Marked as answer by wikky2007 Friday, August 2, 2013 1:39 PM
    Friday, August 2, 2013 1:39 PM