The Zurich release has arrived! Interested in new features and functionalities? Click here for more

On submit Client script

Rahul Raja Sami
Tera Guru
Hi
I am using this on submit client to check the conditions with the concerned script include and to validate and pop up the error message if it matches and abort the submission of request.
but this is populating the error message but not aborting the action.
I have tried return false but no use.
how to solve this?
 

 

function onSubmit() {
    // Retrieve the variable values
    var requestedFor = g_form.getValue('requestor'); // Replace with the actual variable name
    var spaceName = g_form.getValue('u_ref_name_of_qlik_sense_space'); // Replace with the actual variable name
    var accessType = g_form.getValue('u_sb_access_level_required'); // Replace with the actual variable name

    var isValid = false;
    // Call the Script Include to check for duplicates
    var ga = new GlideAjax('RITMValidation');
    ga.addParam('sysparm_name', 'checkDuplicateRITM');
    ga.addParam('sysparm_requested_for', requestedFor);
    ga.addParam('sysparm_space_name', spaceName);
    ga.addParam('sysparm_access_type', accessType);

    ga.getXMLAnswer(function(response) {
        //var answer = response.responseXML.documentElement.getAttribute('answer');
        var answer = response;
        if (answer === 'duplicate') {
            g_form.addErrorMessage('A request with the same details already exists.');
            // return false; // Prevent form submission
            //g_form.submit(false);
        } else {
            isValid = true;
        }

        return isValid;
    });

}



Thanks

 

1 ACCEPTED SOLUTION

@Rahul Raja Sami 

So are you saying it's working as expected?

if not then check how to handle onSubmit with GlideAjax and perform the validation which will work in both native + portal

Refer these links for workaround/solution on how to use Synchronous GlideAjax in onSubmit

How To: Async GlideAjax in an onSubmit script

Asynchronous onSubmit Catalog/Client Scripts in ServiceNow

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

9 REPLIES 9

@Rahul Raja Sami 

that's what I mentioned the form will get submitted and won't wait for the script include function

check the links which I shared above

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Rahul Raja Sami 

Hope you are doing good.

Did my reply answer your question?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi Ankur, yes it worked. Thanks for the solution.

IronPotato
Mega Sage

Hi @Rahul Raja Sami ,

 

you are returning nothing from onSubmit function. You only returning value from XMLAnswer but onSubmit function it self is returning nothing.

 

IronPotato_0-1737466964257.png

 

Please see updated script:

 

function onSubmit() {
  // Retrieve the variable values
  var requestedFor = g_form.getValue("requestor"); // Replace with the actual variable name
  var spaceName = g_form.getValue("u_ref_name_of_qlik_sense_space"); // Replace with the actual variable name
  var accessType = g_form.getValue("u_sb_access_level_required"); // Replace with the actual variable name

  var isValid;
  // Call the Script Include to check for duplicates
  var ga = new GlideAjax("RITMValidation");
  ga.addParam("sysparm_name", "checkDuplicateRITM");
  ga.addParam("sysparm_requested_for", requestedFor);
  ga.addParam("sysparm_space_name", spaceName);
  ga.addParam("sysparm_access_type", accessType);

  ga.getXMLAnswer(function (response) {
    //var answer = response.responseXML.documentElement.getAttribute('answer');
    var answer = response;
    if (answer === "duplicate") {
      g_form.addErrorMessage("A request with the same details already exists.");
      isValid = false;
    } else {
      isValid = true;
    }
  });

  return isValid;
}
 
If my answer helped you in any way, please then mark it as helpful and correct. This will help others finding a solution.

this is not working just displaying the error message but not aborting the submission.