Grtting error on g_scratchpad using in onsubmit client script(catalog item)

Venky Kshatriy2
Tera Contributor

Hi team,

 

i am calling script include in on submit client script and i used the g_scratchpad method i am getting below error

 

onSubmit script error: ReferenceError: g_scratchpad is not defined:
function () { [native code] }

 

 

function onSubmit() {
    //Type appropriate comment here, and begin script below

    if (g_scratchpad.validForm) {
        return;
    }


    var partner = g_form.getValue('select_partner');
    var segment = g_form.getValue('segment_lead');

    var mrvs = g_form.getValue('itinerary');

    var vk = new GlideAjax("Decision_table_risk_rating");
    vk.addParam('sysparm_name', 'getriskratings');
    vk.addParam('sysparm_from', g_form.getValue('from_originating_country'));
    vk.addParam('sysparam_to', g_form.getValue('to_destination_country'));
    vk.addParam('sysparam_mrvs', mrvs);

    vk.getXML(function risk(response) {

        var answer = response.responseXML.documentElement.getAttribute("answer");
        alert('risk_rating' + answer);
        g_form.setValue('risk_rating', answer);
        if (answer == 1 && partner == '' && segment == '') {
            g_form.setVisible('partner', true);
            g_form.setVisible('segment', true);
            g_form.setMandatory('partner', true);
            g_form.setMandatory('segment', true);
            g_scratchpad.validForm = false;
        } else {
            g_scratchpad.validForm = true;
            g_form.submit();
        }
    });
5 REPLIES 5

Vrushali  Kolte
Mega Sage

Hello @Venky Kshatriy2 ,

 

Did you declare and assign any value to g_scratchpad variable in any other script like BR or other client script?

You should be declaring and assigning some value to g_scratchpad variable before accessing it.

 

Please refer below links for more details about g_scratchpad variable-

https://www.servicenow.com/community/developer-forum/how-we-can-use-g-scratchpad-in-business-rule-an...

https://www.servicenow.com/community/itsm-articles/quot-use-of-server-side-script-g-scratchpad-and-c...

 

 

If my answer solves your issue, please mark it as Accepted✔️ and Helpful👍 !

 

 

Hi @Vrushali Kolte ,

 

Same script i used in my PDI it's working fine one instance it's working one instance it's not working .I unable to find the error. if know anything help me to resolve this issue

OlaN
Giga Sage
Giga Sage

Hi,

You cannot use GlideAjax methods in an onSubmit client script.

GlideAjax runs async, and so the result back from the method is retrieved after the submit has already been processed.

If you need to validate some input on the form using GlideAjax, it must be done with an onChange client script.

Chaitanya Redd1
Tera Guru

Hello,

g_scratchpad object is available only in server-side scripts, and it is used to pass data from the server to the client script. If you need to pass data from the server to the client, you should use a GlideAjax call to fetch the data from the server-side

Create a script include named Decision_table_risk_rating and make sure it's client-callable.

 

var Decision_table_risk_rating = Class.create();
Decision_table_risk_rating.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getriskratings: function() {
        var fromOriginatingCountry = this.getParameter('sysparm_from');
        var toDestinationCountry = this.getParameter('sysparam_to');
        var mrvs = this.getParameter('sysparam_mrvs');

        // Perform your logic here to determine the risk rating
        var riskRating = // logic to calculate risk rating;

        return riskRating;
    }
});

 

 

Modify your onSubmit client script to use the GlideAjax call correctly

 

function onSubmit() {
    var partner = g_form.getValue('select_partner');
    var segment = g_form.getValue('segment_lead');
    var mrvs = g_form.getValue('itinerary');

    var vk = new GlideAjax('Decision_table_risk_rating');
    vk.addParam('sysparm_name', 'getriskratings');
    vk.addParam('sysparm_from', g_form.getValue('from_originating_country'));
    vk.addParam('sysparam_to', g_form.getValue('to_destination_country'));
    vk.addParam('sysparam_mrvs', mrvs);

    vk.getXMLAnswer(function(answer) {
        alert('risk_rating: ' + answer);
        g_form.setValue('risk_rating', answer);

        if (answer == '1' && !partner && !segment) {
            g_form.setVisible('partner', true);
            g_form.setVisible('segment', true);
            g_form.setMandatory('partner', true);
            g_form.setMandatory('segment', true);
            return false; // Prevent form submission
        } else {
            return true; // Allow form submission
        }
    });

    return false; // Prevent form submission until GlideAjax call completes
}

 

 

Regards,

Chaitanya