how we can use g_scratchpad in business rule and workflow ?

Savita P
Tera Contributor

how we can use g_scratchpad in business rule and workflow ?

1 ACCEPTED SOLUTION

AbhishekGardade
Giga Sage

Hello Hit,

#g_scratchpad:  The g_scratchpad object passes information from the server to the client, such as when the client requires information not available on the form. For example, if you have a client script that needs to access the field u_retrieve, and the field is not on the form, the data is not available to the client script. A typical solution to this situation is to place the field on the form and then always hide it with a client script or UI policy. While this solution may be faster to configure, it is slower to execute.

 

  1. The g_scratchpad is an empty object you can use to push information (properties, objects, functions, etc.) from the server to the client. It is only available in display business rules and client scripts.
  2. It’s a one directional process, server to client
  3. It is only available in display business rules and client scripts. I use it a lot to populate information on the form that is typically unavailable without an Ajax call.
  4. Workflow activities:
    1. scratchpad.variableName = variableValue;
    2. var myValue = workflow.scratchpad.variableName;

Example:

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

g_scratchpad.css = gs.getProperty('css.base.color');
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);

For detailed information about it:

https://servicenowgems.com/2016/10/10/understanding-scratchpad-g_scratchpad/

Please mark as Correct Answer/Helpful, if applicable.
Thanks!
Abhishek Gardade

 

Thank you,
Abhishek Gardade

View solution in original post

5 REPLIES 5

Allen Andreas
Administrator
Administrator

Hi,

g_scratchpad is used in Display Business Rules and used in conjuction with a Client Script/UI Policy and basically is a workaround method for calling the server in a client without using a Script Include. More so used to log what the value of the record was onLoad so no matter what the user changes stuff too, you can reference the current saved records original values.

workflow.scratchpad is used in Workflows.

They aren't able to be used across one another, directly.

See this thread for more details: https://community.servicenow.com/community?id=community_question&sys_id=62200761db98dbc01dcaf3231f96...

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


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

Glad you found an answer.


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

Harish KM
Kilo Patron
Kilo Patron

Example below

1. g_scratchpad using display Business rule

I want to check the logged in user group and if he is a member of group. I want to show certain fields on incident form. So I write a display BR on Incident table

SCRIPT:

(function executeRule(current, previous /*null when async*/) {

g_scratchpad.grp = gs.getUser().isMemberOf('d625dccec0a8016700a222a0f7900d06'); // Pass the sysid of ABC group

})(current, previous);

 

Client script on Incident table

function onLoad() {

if(g_scratchpad.grp) {
g_form.setDisplay('category',true); // if he is part of group make category field visible
}
else
{
g_form.setDisplay('category',false); // if he is not part of group hide category field
}
}

 

example 2. workflow scratchpad variable. We can use this scratchpad to pass values within the workflow

I have a routing table , based on this table I want to set the assignment group for task

run script 

//find Routing for Task assignment group

var req_group = '';
var gr = new GlideRecord("routingtablename");
gr.addQuery('u_catalog_item', '4bb2739a4f865700c2c3ab99f110c7f7'); //Item sysid
gr.addQuery("u_country", current.variables.caller.u_country);// I want callers country
gr.query();
if (gr.next()) {
req_group = gr.u_task_1; // assign the group
}

workflow.scratchpad.assignment_group = req_group; // I will hold this value in scratchpad to pass it to any activity within the workflow

so when I create a task in workflow I can use the above scratchpad variable to set the assignment group on task

Run script to create task

//Insert task 
var task = new GlideRecord("sc_task");
task.initialize();
task.parent = current.sys_id;
task.assignment_group = workflow.scratchpad.assignment_group; //Here I can pass the scratchpad value to assignment group
var sysID = task.insert();

Hope this helps you

 

Regards
Harish

AbhishekGardade
Giga Sage

Hello Hit,

#g_scratchpad:  The g_scratchpad object passes information from the server to the client, such as when the client requires information not available on the form. For example, if you have a client script that needs to access the field u_retrieve, and the field is not on the form, the data is not available to the client script. A typical solution to this situation is to place the field on the form and then always hide it with a client script or UI policy. While this solution may be faster to configure, it is slower to execute.

 

  1. The g_scratchpad is an empty object you can use to push information (properties, objects, functions, etc.) from the server to the client. It is only available in display business rules and client scripts.
  2. It’s a one directional process, server to client
  3. It is only available in display business rules and client scripts. I use it a lot to populate information on the form that is typically unavailable without an Ajax call.
  4. Workflow activities:
    1. scratchpad.variableName = variableValue;
    2. var myValue = workflow.scratchpad.variableName;

Example:

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

g_scratchpad.css = gs.getProperty('css.base.color');
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);

For detailed information about it:

https://servicenowgems.com/2016/10/10/understanding-scratchpad-g_scratchpad/

Please mark as Correct Answer/Helpful, if applicable.
Thanks!
Abhishek Gardade

 

Thank you,
Abhishek Gardade