g_scratchpad in catalog client script

Sourin1
Tera Contributor

Hello All,

I was going through onSubmit catalog client script code written by a developer. I found that "g_scratchpad" is used in the script. I want to know what is the use case of "g_scratchpad" in catalog script. If anyone have any idea please revert. I am pasting the code below.

Thanks.

------------------------------------------------------------------------Code----------------------------------------------------------------------------

function onSubmit() {
    //Type appropriate comment here, and begin script below
    // alert(g_scratchpad._ajaxChecked);
    var g_scratchpad = g_scratchpad || NOW;
    if (g_scratchpad._ajaxChecked) {
        // We have run our Ajax Checks, so we can continue on
        // and let our form submission continue
        g_scratchpad._ajaxChecked = null;
        return true;
    }

    // use this line below if you want to store the specific action name
    g_scratchpad._action = g_form.getActionName();
    g_scratchpad._ajaxChecked = false;
    
    // alert(g_scratchpad._action);

    //Fill Perimeter List

    var check_prod = g_form.getValue("PROD");
    var check_sdbx = g_form.getValue("SDBX");
    var check_shrd = g_form.getValue("SHRD");
    var check_hprd = g_form.getValue("HPRD");

    var string = '';


    if (check_prod == 'true')
        string = 'PROD';

    if (check_sdbx == 'true')
        if (string != '')
            string = string + ';' + 'SDBX';
        else
            string = 'SDBX';

    if (check_shrd == 'true')
        if (string != '')
            string = string + ';' + 'SHRD';
        else
            string = 'SHRD';

    if (check_hprd == 'true')
        if (string != '')
            string = string + ';' + 'HPRD';
        else
            string = 'HPRD';


    //alert(string);

    g_form.setValue('perimeter_list', string);

    // Fill Group Name
    var name;
    var ga = new GlideAjax('ItemCreateGrpOrch');
    ga.addParam('sysparm_name', 'getGroupApp');
    ga.addParam('sysparm_app', g_form.getValue("application_name"));
    ga.addParam('sysparm_perimeter', g_form.getValue("perimeter_list"));
    ga.addParam('sysparm_zone', g_form.getValue("zone"));
    ga.addParam('sysparm_domain', g_form.getValue("domain"));
    ga.addParam('sysparm_product', g_form.getValue("product"));
    ga.addParam('sysparm_role', g_form.getValue("role"));
    ga.getXMLAnswer(function(response) {
                // if (answer == 'false') {
                var canSubmit = true;

                var answer = JSON.parse(response);
               /*
               alert('name ' + answer.name);
                alert('large ' + answer.large);
                alert('glarge ' + answer.glarge);
                alert('owner ' + answer.owner);
                */

                g_form.setValue('grplarge', answer.glarge.toString());
                g_form.setValue('grpname', answer.name.toString());
                g_form.setValue('listOwner', answer.owner.toString());

                if(answer.large == true) {
                    alert("Some groups are too larges, Maximum charactere is 64 !");
                    canSubmit = false;
                }
                if(canSubmit){
                    g_scratchpad._ajaxChecked = true;
                    if (typeof g_form.orderNow != 'undefined') {
                        // this is a catalog item
                        g_form.orderNow();
                    } else {
                        // this will resubmit the form using the saved 
                        // ui action that was originally clicked
                        g_form.submit(g_scratchpad._action);
                    }
                }         

 });
  // always return false if we get to this point
  return false;
}

8 REPLIES 8

Mohith Devatte
Tera Sage
Tera Sage

Hi,

The g_scratchpad object passes information from the server to the client, such as when the client requires information not available on the form. 

 

If you know what information the client needs from the server before the form is loaded, a display business rule can create g_scratchpad properties to hold this information. The g_scratchpad is sent to the client when the form is requested, making it available to all client-side scripting methods. This is a very efficient means of sending information from the server to the client. However, you can only load data this way when the form is loaded. The business rule cannot be triggered dynamically. In those cases, use an asynchronous GlideAjax call.

 

For example, assume you open an incident and need to pass this information to the client:

 

 

  • Whether or not the current record has attachments
  • The name of the caller's manager

A display business rule sends this information to the client using the following script:

 

 

g_scratchpad.hasAttachments = current.hasAttachments();

g_scratchpad.managerName     = current.caller_id.manager.getDisplayValue();

 

To access scratchpad data using a client script:

 

// Check if the form has attachments

 if (g_scratchpad.hasAttachments)

  // do something interesting here

 else

 alert('You need to attach a form signed by ' + g_scratchpad.managerName);

 

Please mark answer as Correct if it answers your question.

Thanks ,

Mohith Devatte

Allen Andreas
Administrator
Administrator

Hi,

It's best that you search for this information as a simple google search or documentation search brings you to this: https://docs.servicenow.com/en-US/bundle/sandiego-application-development/page/script/business-rules...

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Sourin1
Tera Contributor

Hi @Mohith Devatte  & @Allen Andreas,

Thanks for your quick and detailed response. I know what a g_scratchpad object does and how to use that. My question is I have never seen anybody until now use a g_scratchpad in a catalog client script that too on a variable set, and also I didn't find any BR defining the value for the variables.

2 things I want to know,

1. On which table is the BR written in this case, since g_scratchpad is called in catalog client script.

2. Is there any other way to use g_scratchpad

I am attaching a link that I found somewhat relevant to my problem, If you can please refer to this and understood what is actually done, it would be a great help if you can explain that to me.

https://community.servicenow.com/community?id=community_question&sys_id=8a7075ff1bfe8110587a11751a4b...

Thanks.

Hi,

It was not clear that you knew what g_scratchpad was or does as you said this:

"I want to know what is the use case of "g_scratchpad" in catalog script."

To me, that most likely means you don't know what it is or does and you'd like to some help, which is why I replied with what I did.

Moving on...you can't use g_scratchpad in catalog client script. So, the code that is there may not really even be working.

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!