Is it possible to access a record's workflow scratchpad from a script run on different record?

kyleb13281
Kilo Expert

Is it possible to access a record's workflow scratchpad from a script run on different record?

Trying to access the scratchpad on an RITM record from an approval record.

Thanks!

Kyle

2 REPLIES 2

TrevorK
Kilo Sage

I believe so. I have used the following code from a UI Page to retrieve what is in the scratchpad (the result of a powershell action). This is part of a function, so "CS" is the actual SYSID I am passing into it.



You can ignore the "replace" bit, I think I have that in there because what is returned to me has some stuff in it I need to parse out. I am sure there is a much better way of dealing with it, but I only had one variable to return so that was easiest for me. Otherwise, the logic for how to call the workflow's scratchpad should be there. I do not see why this could not be done anywhere (such as your example of looking at it from the approval record).



Hope this helps. If you have any questions let me know, but I think with the code below you will have it.



[i]
var Call_SysID = CS;


var WFScratchpad = new GlideRecord('wf_context');


WFScratchpad.addQuery('id',Call_SysID);


WFScratchpad.query();



if (WFScratchpad.next()) {


  var pswd_scratchpad = WFScratchpad.scratchpad;


 


  // replaceAll did not seem to work. When I use the { without the ' in front it throws a warning (but lets you proceed).


  // Just to keep the code warning free I am using the ' to wrap { and }


  pswd_scratchpad = pswd_scratchpad.replace('{','');


  pswd_scratchpad = pswd_scratchpad.replace('}','');


  pswd_scratchpad = pswd_scratchpad.replace(/"/g,'');


  pswd_scratchpad = pswd_scratchpad.replace('password:','The new password is: ');


  alert(pswd_scratchpad);


}


[/i]


nikita_mironov
Kilo Guru

Better do that using ServiceNow API:

https://docs.servicenow.com/bundle/london-application-development/page/script/useful-scripts/referen...

//the run script activity sets a value in the scratchpad
workflow.scratchpad.important_msg = "scratch me";
 
//get the workflow script include helper 
var workflow = new Workflow();
 
//get the requested items workflow context 
//this will get all contexts so you'll need to get the proper one if you have multiple workflows for a record 
var context = workflow.getContexts(current); 
//make sure we have a valid context 
if (context.next()) { 
  //get a value from the scratchpad 
  var msg = context.scratchpad.important_msg; 
  //msg now equals "scratch me", that was set in the run script activity
 
  //add or modify a scratchpad value
  context.scratchpad.status = "completed"; 
  //we need to save the context record to save the scratchpad
  context.update(); 
}