Why is g_scratchpad used in onSubmit Catalogue Client Script?

matthew_hughes
Kilo Sage

I've just been looking at the onSubmit below Catalogue client Script:

 
function onSubmit() {

    // If we already validated, continue
    if (g_scratchpad.isFormValid) {
        g_scratchpad.isFormValid = false;
        return true;
    }

    // // get the system property value that holds the names of all the checkboxes
    var gaProperty = new GlideAjax('sn_apm_di.LBGDigitalInterfacesURLUtils');
    gaProperty.addParam('sysparm_name', 'getSystemProperty');
    gaProperty.addParam('sysparm_propertyName', 'sn_apm_manage_bus_app_fields_catalog_item_checkbox_names');
    gaProperty.getXML(processCheckboxes);
    return false;

}

function processCheckboxes(response) {
    // get the string of checkbox names
    var answer = response.responseXML.documentElement.getAttribute("answer");
   
    // convert to an array
    var aryCheckboxes = answer.split(",");
   
    // det the default status that none have been selected
    blnSelectionMade = false;

    // loop through all the checkboxes
    for(var loop = 0; loop < aryCheckboxes.length; loop++){
        // if we find one that is selected
        if(g_form.getBooleanValue(aryCheckboxes[loop])){
            //update our variable
            blnSelectionMade = true;
            // break out from the loop
            break;
        }
    }

    //If a checkbox is selected, then allow the request to be submitted. Otherwise show the below error message
    if(blnSelectionMade){
        g_scratchpad.isFormValid = true;
        g_form.submit("submit");        
    } else {
        g_form.addErrorMessage('It looks like you have not changed any data. Please select a data point to update it.');
    }
}
 
However, what I've noticed in the catalogue item itself is that 'g_scratchpad' is not declared as a variable.
 
So I just wondering if anyone knows why 'g_scratchpad' is used in this catalogue client script and where the value comes from?
4 REPLIES 4

Dhanraj B
Giga Guru

Basically, g_scratchpad will be declared in display business rules to pass the information from Server side to client side. It will not be declared in the client script. You can look at the business rules of the table where the form is being viewed. 

 

-Dhanraj.

Ankur Bawiskar
Tera Patron

@matthew_hughes 

in this case it's basically used to handle synchronous GlideAjax

Reason being Synchronous GlideAjax is not allowed in portal and in scoped app

Async Validation in onSubmit Catalog Client Script in Service Portal and Synchronous Validation in N... 

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

nayanmule
Kilo Sage

@matthew_hughes  , g_scratchpad isn't any catalog variable or a script variable to be declared in client scripts.

It's basically used to fetch the data from Server side especially from Display Business Rules.

I am sure in your script - g_scratchpad.isFormValid is already declared in your business rules to fetch some value and then pass it into your Onsubmit Client script.

 

To learn more about scratchpad , you may go through these docs, articles to get a better understanding - 

https://www.servicenow.com/community/developer-forum/what-is-scratchpad/m-p/2118432 

https://www.servicenow.com/community/developer-forum/explain-scratchpad/m-p/2256541 

 

If my response has helped you, kindly mark it as helpful and accept the solution.

Regards,

Nayan

adityahubli
Tera Guru

Hello @matthew_hughes ,

 

Generally g_scratchpad is used to pass data from server side to client side using Display BR . Im not getting why you are using g_scratchpad as well as glideajax both at time .GlideAjax is also used to fetch server side data to client side using Script include . g_scratchpad is a built-in object provided by ServiceNow, so you don’t need to declare it in the catalog client script. As per my understanding ,it is automatically available on the client side and is commonly used to temporarily store values while a form is open but you need to declare it in display BR means you just need to initialized it in BR. Then you can call that g_scratchpad property in client script .

 

 

Im not sure but here based on you glideajax g_scratchpad property value is getting set . is it working ?