The CreatorCon Call for Content is officially open! Get started here.

Prevent the Submission of Catalog after the false response from Script Incude

NehaSNOW
Tera Guru

Hi Members,

 

Below is the Catalag Client Script on onSubmit.

Requirement: - I want to prevent the submission of the Catalog Item if the response from the GlideAjax is false i.e. answer == 'false'. Whereas, the Catalog Item should be submitted if the response from the GlideAjax is true i.e. answer == 'true'.

 

Please suggest. 

 

function onSubmit() {
    // Get the values from the form
    var group_name = g_form.getValue('describe_the_change_required_on_the_group');
    var catsys_id = g_form.getUniqueValue();

    // Create the GlideAjax call to get the keyword
    var catKey = new GlideAjax('CatalogKeyword');
    catKey.addParam('sysparm_name', 'getKeyword');
    catKey.addParam('sysparm_cat_sysid', catsys_id);
    catKey.addParam('sysparm_keyword_name', 'group_name_keyword');
    catKey.addParam('sysparm_group_name', group_name);

    // Start the asynchronous GlideAjax call
    catKey.getXMLAnswer(getCatalogKeywordResponse);

    // Prevent form submission until we get the response
    return false;

    // This is the callback function to process the response
    function getCatalogKeywordResponse(answer) {
        g_form.addInfoMessage('answer: ',answer);
        if (answer == 'false') {
            // Validation failed, show an error and prevent form submission
            g_form.addFormMessage('Amazon and AZ keywords are not allowed.','error');
            return false;  // Prevent form submission
        }
       
        return true;
        // If validation passes, submit the form
        //g_scratchpad.isFormValid = true;
        //g_form.submit();  // Submit the form if validation passes
    }
}
3 REPLIES 3

Shashank_Jain
Kilo Sage

@NehaSNOW  , Try the below code and make sure your script include returning correct output.

 

function onSubmit() {
    var groupName = g_form.getValue('describe_the_change_required_on_the_group');
    var catSysId = g_form.getUniqueValue();

    // GlideAjax call
    var ga = new GlideAjax('CatalogKeyword');
    ga.addParam('sysparm_name', 'getKeyword');
    ga.addParam('sysparm_cat_sysid', catSysId);
    ga.addParam('sysparm_keyword_name', 'group_name_keyword');
    ga.addParam('sysparm_group_name', groupName);

    // Async call with callback
    ga.getXMLAnswer(function(answer) {
        if (answer === 'false') {
            g_form.addErrorMessage('Amazon and AZ keywords are not allowed.');
        } else if (answer === 'true') {
            g_form.submit(); // Resubmit if valid
        } else {
            g_form.addErrorMessage('Validation failed. Please try again.');
        }
    });

    // Always block until Ajax callback decides
    return false;
}
If this works, please mark it as helpful/accepted — it keeps me motivated and helps others find solutions.
Shashank Jain

@Shashank_Jain Thanks for your response. Issue is it is block me to go to the Next page to submit the REQ when the answer === 'true'. PFA.

 

NehaSNOW_2-1755625872948.png

 

 

@NehaSNOW , Try this!

 

var isValidationInProgress = false;
var isValidationPassed = false;

function onSubmit() {
    // If validation already passed once → allow submission
    if (isValidationPassed) {
        return true;
    }

    // Prevent multiple Ajax calls
    if (isValidationInProgress) {
        return false;
    }

    isValidationInProgress = true;

    var groupName = g_form.getValue('describe_the_change_required_on_the_group');
    var catSysId = g_form.getUniqueValue();

    var ga = new GlideAjax('CatalogKeyword');
    ga.addParam('sysparm_name', 'getKeyword');
    ga.addParam('sysparm_cat_sysid', catSysId);
    ga.addParam('sysparm_keyword_name', 'group_name_keyword');
    ga.addParam('sysparm_group_name', groupName);

    ga.getXMLAnswer(function(answer) {
        isValidationInProgress = false; // reset flag

        if (answer === 'false') {
            g_form.addErrorMessage('Amazon and AZ keywords are not allowed.');
            // stay blocked
        } else if (answer === 'true') {
            isValidationPassed = true; // mark validation success
            g_form.submit(); // submit again → this time bypasses validation
        } else {
            g_form.addErrorMessage('Validation failed. Please try again.');
        }
    });

    return false; // block until Ajax decides
}
If this works, please mark it as helpful/accepted — it keeps me motivated and helps others find solutions.
Shashank Jain