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

Ankur Bawiskar
Tera Patron
Tera Patron

@Rahul Raja Sami 

this is for catalog form?

If yes then the async GlideAjax won't work in native or portal as the form will get submitted by the time the script include function performs the validation

please share some more details

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

yes this is for catalog item request.
so when a user tries to submit a request by filling some fields, my requirement is to validate those fields with already submitted RITM variable values and display error message mentioning a request has already been submitted for these details. 
So I am using a script include and on submit client script for this, On Submit CS fetches the values of the new request and sends to SC for validation and SC returns whether it is duplicate or not. Then CS pops up an error message and aborts the submission.  I have tried the following CS as well

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

    // 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.getXML(function(response) {
        var answer = response.responseXML.documentElement.getAttribute('answer');
        if (answer === 'duplicate') {
            g_form.addErrorMessage('A request with the same details already exists.');
            return false; // Prevent form submission
           
        } else {
            return true;
        }

    });

}

@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

no Ankur, just the error message is populating but not aborting the submission.