locked
OnLoad is being triggered and should not be RRS feed

  • Question

  • I have the following code that is Triggered on the OnSave event of the Form and the works perfect for new cases:

    function ForceRequirement(executionObj){
           if (Xrm.Page.getAttribute("casetypecode").getText() === "Problem")   {
                     var formType = Xrm.Page.ui.getFormType();
                     var canSave = false;
                     var fields = ["new_checkbox1a", "new_checkbox2a", "new_checkbox3a","new_checkbox4a", "new_checkbox5a", "new_checkbox6a", "new_checkbox7a"];

                     for (index = 0; index < fields.length; index++)   {
                              var checkboxValue = Xrm.Page.getAttribute(fields[index]).getValue();

                              if (checkboxValue == true)  {
                                       canSave = true;
                                       break;
                             }
                    }

              // stop the save event 

             if (canSave == false)   {
                   alert("You must select an option in the Significant Anomaly Evaluation section.");
                    executionObj.getEventArgs().preventDefault();
             }

          }

    }

    However, on the OnLoad event of a saved OLD CASE (opening an exiting case to Edit) it goes through the entire code 4 times when it is a "Problem"
    It should only trigger the alert when clicking OnSave event

    This is occurring because some other function on the onLoad event trigger the SAVE evet befor the form is actually save cuasing the behaviour above.

    I cannot figure how to get this to work not to prompt the alert when opening an existing Case but still want it to be triggered on the OnSave.

    Hope this is clear, thanks for any help.


    Nick M



    Monday, September 14, 2015 6:30 PM

Answers

  • 1.Make a global variable like (callForceRequirement).

    2. Assign false this as false from your onload function.

    3. assign true at the end of your ForceRequirement function.

    4. make a check in  function ForceRequirement. show alert if callForceRequirement is true.

     

    Hope this helps. ----------------------------------------------------------------------- Santosh Bhagat If this post answers your question, please click "Mark As Answer" on the post and "Vote as Helpful"

    Tuesday, September 15, 2015 9:41 AM

All replies

  • 1.Make a global variable like (callForceRequirement).

    2. Assign false this as false from your onload function.

    3. assign true at the end of your ForceRequirement function.

    4. make a check in  function ForceRequirement. show alert if callForceRequirement is true.

     

    Hope this helps. ----------------------------------------------------------------------- Santosh Bhagat If this post answers your question, please click "Mark As Answer" on the post and "Vote as Helpful"

    Tuesday, September 15, 2015 9:41 AM
  • Thanks Santosh.

    That should work but late yesterday I did the following that also works,

    I added an AND to the if statement and that seems to have resolved my issue.

    if ((Xrm.Page.getAttribute("casetypecode").getText() === "Problem") &&  (Xrm.Page.data.entity.getIsDirty() ==true))


    Nick M

    Tuesday, September 15, 2015 12:37 PM