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?
9 REPLIES 9

@matthew_hughes 

that's correct it doesn't explain

it's just a workaround many developers use to handle sync GlideAjax in portal and scoped app

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

Hi @Ankur Bawiskar So we don't know why scratchpad is used in a client script?

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

Aditya_hublikar
Kilo Sage

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 ?

 

 

Its_Sagnic
Mega Guru

Hi @matthew_hughes ,

Hope you are doing well.

In your script, g_scratchpad is used as a submission flag to handle the asynchronous nature of GlideAjax. 
The primary issue is that g_scratchpad is not officially supported for Service Catalog forms in all versions. In your code, it acts as a "memo" so that when the form re-submits itself after the validation, the script knows it has already passed the check. 

Instead you may use the below code snippet that may work for you .

function onSubmit() {
    // 1. Check if we already validated in this session
    if (window.isFormValid) {
        window.isFormValid = false; // Reset for next time
        return true;
    }

    // 2. Prevent submission immediately to wait for Ajax
    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.getXMLAnswer(function(answer) {
        var aryCheckboxes = answer.split(",");
        var blnSelectionMade = false;

        for (var i = 0; i < aryCheckboxes.length; i++) {
            if (g_form.getBooleanValue(aryCheckboxes[i].trim())) {
                blnSelectionMade = true;
                break;
            }
        }

        if (blnSelectionMade) {
            // 3. Mark as valid and trigger submission again
            window.isFormValid = true;
            // Use g_form.submit() for Native UI or orderNow for Portal
            if (typeof g_form.orderNow === 'function') {
                g_form.orderNow(); 
            } else {
                g_form.submit();
            }
        } else {
            g_form.addErrorMessage('It looks like you have not changed any data. Please select a data point to update it.');
        }
    });

    return false; // Stop the initial submission
}

check that above code I added some flag in-between the code to make it easy for you to understand the execution.

If the solution is useful for you please mark it as helpful and accept the solution to close the thread.

Regards,

Sagnic