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

Hi, I believe there is a way to use g_scratchpad in catalog client scripts:

 

If you're running a catalog client script in the portal UI, g_scratchpad should work just fine and can be accessed in other scripts.

 

If the script is running in the desktop UI, g_scratchpad doesn't work. I've previously used g_form.attribute_name for asynchronous onsubmit scripts but using this method will only show the the updates to the g_form object if you call it from within the same script.

 

However, I have managed to use window.attribute_name in the desktop UI which does allow you to share values between multiple scripts and works exactly the same as g_scratchpad (although I have no idea whether or not this would be considered best practice).

 

Hope this helps 🙂

Hello,

I'm unsure if you've tested this as you're saying "should work", etc. but to reiterate what I mentioned almost 2 years ago, using scratchpad for catalog items doesn't work. And doesn't work today, from my testing.

 

AllenAndreas_0-1713445630469.png

AllenAndreas_1-1713445649887.png

AllenAndreas_2-1713445667503.png

AllenAndreas_3-1713445690815.png

 

 

Please share more information as to how you're getting to it work.

 

Thanks!


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

Hi, I say "should work" as there could be differences in system properties etc between our instances that may mean it doesnt work for others - I have tested it and it is working for me.

 

I see you've used an on display business rule running on the catalog item table which I havent tried -instead, I used an onload client script which makes a glideajax call and sets the scratchpad depending on the UI type as below: 

if (window == null) {
// runs in portal UI
        g_scratchpad.variable_name = 'true';
    } else {
// runs in desktop UI
        window.variable_name = 'true';
    }
 
Additionally, in what UI are you testing your client script? As I mentioned, g_scratchpad only works when viewing a catalog item in the portal UI and so if testing on desktop, your script wont work and you'd have to use window.user instead.
 
I have an onchange client script running for both UI types (as my catalog item is available in the portal and desktop UI view) which retrieves the value from either scratchpad or window depending on the UI type:
var a = window == null ? g_scratchpad.variable_name : window.variable_name;
 
Hope this helps/ works for you.

Hi,

I don't believe there'd be any system property that would effect the scratchpad from working in one instance versus another, but in any case...yes a display business rule was used as that is what 99% of people would use to set a scratchpad object and what was discussed above.

 

If you had an alternative to that, such as answering #2 from the above person, then it would definitely be important for you to mention that you're using GlideAjax to call the server to then set a scratchpad object. It wasn't clear from your post that that was your method used. Usually someone needs the data once in a scratchpad object and so if you're using GlideAjax to retrieve the data, that sort of defeats the purpose of the scratchpad, but I can see what you're saying about using it across multiple client scripts, but commonly, that's not really needed, but overall, still good to know! 🙂

 

On a separate note, I did see your comment about it only working on the portal and that is where I had tried and screenshotted above and that is what doesn't work and still doesn't work today.

 

Thanks for providing more information as that benefits the community at large.

 

Take care!


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