When working with the Workflow scratchpad object it is important to note a couple of things:
- Data can be placed on the Scratchpad for later use using the format of workflow.scratchpad.insert_new_var_name_here
- You can create variables on the Scratchpad that are any type (e.g. String, Array, Object, etc). This allows for great flexibility, in my previous post, I demonstrate that you can put an array of objects as a scratchpad variable. By utilizing this method, you have an extensible method to retrieve as much data from the workflow as you would ever need.
- The Scratchpad can be retrieved from external sources such as Business Rules and Script Includes by first grabbing the Workflow’s contexts (script below shows this method):
var req_item_id = 'id you are looking up'; var item = new GlideRecord('sc_req_item'); item.get(req_item_id); workflow.getContexts(item) if (context.next()){ var scratchpad_var = context.scratchpad.your_var_name_here; //do something with the scratchpad data here }
Note, that you can also place data on the Scratchpad using a “Display” Business Rule, and then retrieve on the client side with a Client Script (“onLoad”).
For More information please see the following ServiceNow Wiki articles:
http://wiki.servicenow.com/index.php?title=Using_the_Workflow_Scratchpad#gsc.tab=0
http://wiki.servicenow.com/index.php?title=Accessing_the_Workflow_Scratchpad_from_Business_Rules#gsc.tab=0
Hi, thank you for posting this. It helped me a lot with an issue I was working on regarding storing data in the scratchpad.
Valuable ServiceNow Workflow tip. Lucky me I found your site by accident, saved me a lot of time and effort in dealing with workflows and exchanging data!
I tried the same in script Include,
checkworkflow: function() {
//get the workflow script include helper
var msg = ”;
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
msg = context.scratchpad.important_msg;
//msg now equals “scratch me”, that was set in the run script activity
gs.log(“Hello World “+ msg +’ ‘+current.number);
}
},
Log output is like : Hello World undefined RITM0010051
In RUn Script Activity it is already declared as : workflow.scratchpad.important_msg = ‘showUIaction’;
Hi Shilpa,
Not sure if you got this figured out, but there are a couple of things to consider:
1. You are not passing the “current” object to your “getContexts(current)” function
2. Ensure that “current” is an actual “task” record at the time you’re calling the Script Include
3. Since “msg” is undefined you will want to make sure that your “getContexts(current)” is returning the proper/expected workflow
Hi Max,
Thanks for your reply.
Yes I got the code working. it was to do with the right value being passed to getContext.