Explain scratchpad

Nani7
Tera Contributor

Hi All,

Could someone please explain when, where and why do we scratchpad and some examples. I would like to learn the concept 🙂

 

Regards

5 REPLIES 5

Ragini Kukade
Kilo Guru

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. 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.

 

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:

 

  • The value of the system property css.base.color
  • 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.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);

 

 

Mark it as correct/helpful,if it helps.

Regrads,

Ragini

Service_RNow
Mega Sage

Hi,

scratchpad

The scratchpad in workflow is a space in the workflow context to store and share string based variables (as name:value pairs) between instances of activities within an executing instance of a workflow.

The scratchpad is global to the instance of the running workflow and as such, is available equally to all activities.

Declaration

The scratchpad itself is automatically available to an executing workflow and requires no specific declaration. Variables are declared and stored in the scratchpad simultaneously by referencing it. For example:
workflow.scratchpad.variableName = variableValue;

Display

There is no way to view the contents of the scratchpad.

Access and Assignment

As stated in the Declaration section:
workflow.scratchpad.variableName = variableValue;
or:
var myValue = workflow.scratchpad.variableName;

Mark it as correct/helpful,if it helps.

Thanks

Service_RNow
Mega Sage

Please find the below link and info ,if helpful mark it as helpful and like

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

This is a really old thread, but I'd thought I'd clarify the issue regarding using Objects with g_scratchpad for anyone else who may read the post.

You can create Objects in your Display Business rule and then copy them to the g_scratchpad Object.

 

For example:

  1. var myObject = {name:"Jake",title:"Senior Technical Consultant"}; // create JavaScript Object  
  2. g_scratchpad.myObject = myObject; // copy JavaScript Object to global Object  

This Object would then be available in your Client Script by accessing the scratchpad:

 

  1. alert('Name: '+g_scratchpad.myObject.name);  

The reason you cannot simply copy your User record Object (e.g grTest) to the scratchpad is because it is NOT a JavaScript Object. It is actually a GlideRecord Object, and even the "field" properties of such an Object are not native data types (e.g String, Number, Boolean etc). To successfully copy a GlideRecord Object you would need to first convert it to a JavaScript Object, and then copy that to the g_scratchpad Object in your Display Business Rule. You could do this by type casting (converting) each of the field values from the GlideRecord to String properties in your new Object. For example:

  1. // Display Business Rule  
  2. var userObject; // define null variable/object  
  3. var fieldsToCopy = ['name','email','title']; // array of fieldnames to copy  
  4. var grTest = new GlideRecord('sys_user');  
  5. grTest.addQuery('sys_id','681ccaf9c0a8016400b98a06818d57c7'); // update with dynamic sys_id value  
  6. grTest.query();  
  7. if(grTest.next()) {  
  8.   userObject = {};  
  9.   for(var i=0; i<fieldsToCopy.length; i++){ // loop through fieldname array  
  10.   var fieldName = fieldsToCopy[i];  
  11.   if(grTest[fieldName]) userObject[fieldName] = ''+grTest[fieldName]; // copy field value  
  12.   }  
  13. }  
  14. if(userObject) g_scratchpad.userObject = userObject; // copy Object to scratchpad if not null  

You could further enhance this function by converting some of the fields to either Number or Boolean if need to, but the above script will allow you to copy an "Object" from server to client via g_scratchpad, even if the originating data was a GlideRecord (e.g User record).

Hi Nani,

Please Mark it as correct/helpful, if it helps.

Thanks