On submit client script

Vignesh Raman
Tera Contributor

I have an onSubmit client script that's running on a catalog item. There are 3 fields - A, B, and C. Field A is a reference type field and B and C are single line text. The aim of the catalog client script is a) Prevent submission of the form if the catalog item has the same configuration of the referenced item. b) Allow submission if the configuration is different.
Part a) is working as expected. Part b) is running into an infinite loop. Can someone tell me why this is happening and how it can be resolved? Thanks in advance 🙂
Code added below

function onSubmit() {
    // random alert
    if (g_scratchpad.isCIValid) {
        g_scratchpad.isCIValid = false; // done to reset validation
        // alert 2
        return true;
    }
    var Action = g_form.getActionName();
   
    
    var Fields = new GlideAjax("Script include name");
    Fields.addParam("sysparm_name", "call script include function");
    Fields.addParam("sysparm_1", p1);
    Fields.addParam("sysparm_2", p2);
    Fields.addParam("sysparm_3", p3);

    Fields.getXMLAnswer(callBack);

    function callBack(answer) {
        alert("in ans" + answer);
        if (answer == "false") {
            alert("false");
            g_form.addErrorMessage("You cannot do it this way"); // till this part, it is working
 
        } else {
            g_scratchpad.isCIValid = true; // mark the condition variable checker as true
           //another alert
            g_form.submit(Action); // trigger the form submission
           // this is the point where the script starts malfunctioning and starts showing alert messages in a sequence
           // The alert messages have been commented out, but can someone tell me how to resolve?
            //return true;
        }
    }
    return false;
}

If I remove the g_form.submit(Action), then the form does not get submitted. Can you guide me here? 
3 REPLIES 3

Brad Bowman
Kilo Patron
Kilo Patron

Do not use g_form.submit in an onSubmit client script.  Return false to cancel the form submission, otherwise don't do anything and the onSubmit function will do it's thing (submit the form).  Your return false was outside of the if block.  I'm not seeing the point of isCIValid.  If this is adding value, put the lines back in, but in the simplest format the script looks more like this:

function onSubmit() {
    var Fields = new GlideAjax("Script include name");
    Fields.addParam("sysparm_name", "call script include function");
    Fields.addParam("sysparm_1", p1);
    Fields.addParam("sysparm_2", p2);
    Fields.addParam("sysparm_3", p3);
    Fields.getXMLAnswer(callBack);

    function callBack(answer) {
        alert("in ans" + answer);
        if (answer == "false") {
            g_form.addErrorMessage("You cannot do it this way");
			return false;
        }
    }
}

 

Amit Pandey
Kilo Sage

Hi @Vignesh Raman 

 

I agree with @Brad Bowman  on the usage of g_form.submit in an onSubmit client script as this function triggers the onSubmit() function again, leading to an infinite recursion.

To resolve this issue, you can separate the validation logic from the form submission logic. You can perform the validation synchronously before submitting the form. Can you try this approach-

 

function onSubmit() {
    // Perform synchronous validation first
    var isValid = validateForm();
    
    // If validation passes, submit the form
    if (isValid) {
        return true;
    } else {
        return false;
    }
}

function validateForm() {
    var isValid = false;
    var Fields = new GlideAjax("Script include name");
    Fields.addParam("sysparm_name", "call script include function");
    Fields.addParam("sysparm_1", p1);
    Fields.addParam("sysparm_2", p2);
    Fields.addParam("sysparm_3", p3);

    var answer = Fields.getXMLWait();
    if (answer == "false") {
        g_form.addErrorMessage("You cannot do it this way");
    } else {
        isValid = true;
    }
    
    return isValid;
}

 

 

Please mark my answer helpful and correct.

 

Regards,

Amit

Vignesh Raman
Tera Contributor

Thank you, Brad and Amit for your answers. The issue was caused by another onSubmit client script running at the same time. I managed to resolve this by combining both scripts. Thanks for the input!