Could you Explain This Scratch pad variables and use (Business rule/workflow)

naren446
Kilo Explorer

Could you Explain This   Scratch pad variables and use (Business rule/workflow)

5 REPLIES 5

Goran WitchDoc
ServiceNow Employee
ServiceNow Employee

Hi,



you got 2 different scratchpads



1. The scratchpad that is used to send information between a display business rule and a client script/UI Policy. This is pretty much an alternative to making a GlideAjax Call on the client script instead. If you know you need data that isn't available on the form, you can run the code in a display business rule put the data in the scratchpad and the fetch that data when it's needed in the client script. Just beware that it might be better with GlideAjax in some cases. This since when using scratchpad, you get the data how it looked when the business rule queried it. It might perhaps be 5 min afterwards and for example your onsubmit client script needs the data. Then it can be old data which has been changed.



2. Scratchpad i workflow. This is used to send data between activities. Otherwise you can reach data that was fetch/calclated etc. from a earlier activity.


If Possible, can you please provide the demo code for Point 1.

Please

 

dhananjay
Giga Contributor

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/



Hi Narender



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


Hello Naren,

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

Please refer below links, these might help you:

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

https://community.servicenow.com/community?id=community_question&sys_id=7971cb29db98dbc01dcaf3231f96...

https://www.oreilly.com/library/view/learning-servicenow/9781785883323/cb4b394d-3406-450d-a6e0-9bdb0...

 

If my answer is helpful to you, please mark answer as helpful and correct.

Thanks and regards,

Megha.