logo

NJP

Mini-Lab: Printing off the Workflow Scratchpad object

Import · Jul 30, 2018 · article

NOTE: MY POSTINGS REFLECT MY OWN VIEWS AND DO NOT NECESSARILY REPRESENT THE VIEWS OF MY EMPLOYER, ACCENTURE.

DIFFICULTY LEVEL: INTERMEDIATE
Assumes good knowledge and/or familiarity of Orchestration, Workflows, and Scripting in ServiceNow. Assumes having taken the class SSNF and has good intermediate level of knowledge and/or familiarity with Scripting in ServiceNow.

Here are a couple of neat tricks when developing Service Catalog/Workflow combinations. The first one is to automate the best practice of transferring current.variable items into the workflow.scratchpad object. It is always better to standardize the variable naming and grouping which make it easier to maintain the rest of the workflow. Usually you would do this "manually", but here I introduce a simple method of converting one object into the other. Second, I will show how to simply dump the workflow.scratchpad object at any time to view its contents in the System Log. This is useful while debugging to see what is actually in the data stream of the workflow.

Create the Workflow

1. Open the Workflow Editor and create a new Workflow.

Name: ML - Print Scratchpad Object

Table: Requested Item [sc_req_item]

Description: Demo to print off all properties of the workflow.scratchpad object during run-time.

2. Click on the Submit button to create your workflow.

3. Drag a Run Script Activity out onto the canvas in-between the Begin and End activities.

4. Fill out the form with the following:

Name: Print Scratchpad Object

Script: (read the comments to know what is happening)

var location = context.name + '.' + activity.name;

// convert the current.variables object into the 
// workflow scratchpad object.
// btw, this is the first step to making a workflow Service Catalog variable agnostic.
message = 'current.variables Object: \n';
var variables = current.variables;
for (var item in variables) {
    message += item + ': ' + variables[item] + '\n';
    var name = item.replace('v_','');
    workflow.scratchpad[name] = variables[item];
}
gs.info('---> [{1}] {0}', [message, location]);

// now loop through the workflow scratchpad object and write all the values
// to the System Log.
var message = 'Scratchpad Object: \n';
var scratchpad = workflow.scratchpad; // a little indirection
for (var item in scratchpad) {
    message += item + ': ' + scratchpad[item] + '\n';
}
gs.info('---> [{1}] {0}', [message, location]);

5. Click on the Submit button to save your work.

6. You can go ahead and publish your workflow at this time if you wish.

image

Your flow should look like this:

image

Create the Service Catalog Item

1. Navigate to Service Catalog > Catalog Definitions > Maintain Items

2. Click on the New button to create a new Request Item.

3. Fill out the form with the following:

Name: Scratchpad Variable Sender

Active: checked

Catalogs: Technical Catalog

Category: (anything you want to place it under. I created a new one called Workflow Administration)

image

Under the Item Details tab:

Short description: Send sample variables to a workflow

Under the Process Engine tab:

Workflow: ML - Print Scratchpad Object

Under the Portal Settings tab:

Request Method: Submit

Hide 'Add to Cart': Checked

Hide Quantity: Checked

Hide Delivery Time: Checked

4. Right-click on the form header to bring up the context menu and click on Save to save your work.

View original source

https://www.servicenow.com/community/developer-articles/mini-lab-printing-off-the-workflow-scratchpad-object/ta-p/2330351