sabell2012
Mega Sage
Mega Sage

 

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.

 

sabell2012_1-1702856537612.png

 

Your flow should look like this:

 

sabell2012_2-1702856617094.png

 


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)

 

sabell2012_3-1702856759457.png

 

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.

sabell2012_4-1702857046729.png

 

5. Scroll to the bottom of the form to the related lists.

6. Click on the Variables tab and then click on the New button.

7. Add three new variables:

 

Type: Single Line Text
Question: Test 1
Name: v_test_1
Order: 100

Type: Single Line Text
Question: Test 2
Name: v_test_2
Order: 200

 

Type: Single Line Text
Question: Test 3
Name: v_test_3
Order: 300


Note:  The use of v_ in the naming of Service Catalog Item Variables is considered a best practice for easy identification of such variables.

 

sabell2012_6-1702857292385.png

 

7. Click on the Try It button. This will bring up the new Service Catalog Request form.

8. Fill out the form with any text in the three fields and click the Order Now button.

sabell2012_7-1702857624069.png

 

9. After the Order info form appears navigate to System Logs > System Log > All.

10. Search for '--->' in the messages column.

11. You should see something like this in the logs:

 

sabell2012_8-1702857680413.png

 

You have executed and printed off the Current variables and Workflow Scratchpad objects!

 

Pretty cool, huh?! 😁

 

I will be elaborating more on auto-handling of Service Catalog Item Variables in a future article.

 

Enjoy!

Steven Bell.

 

If you find this article helps you, don't forget to log in and mark it as "Helpful"!

 

sabell2012_0-1702855836140.png


Originally published on: 07-30-2018 12:01 PM

I updated the code, fixed broken links, and brought the article into alignment with my new formatting standard.

Comments
Javier Arroyo
Kilo Guru

Can the scratchpad be printed with?

gs.info( global.JSON.stringify( workflow.scratchpad ) )?

sabell2012
Mega Sage
Mega Sage

Yes!  That will work as well.  It will appear one-lined though.  I do this from time-to-time when I am in a hurry.  I will then copy it out of the log, drop the string in my favorite JSON editor (Notepad++), and format it for viewing.

The purpose of this article was to introduce the Javascript techniques, the auto-population of workflow.scratchpad, and provide a foundation for my next article:

Mini-Lab: Workflow Script Includes

BTW, I will be publishing a few JSON/WF articles starting today, and for the next couple of days.

Steven.

Version history
Last update:
‎12-17-2023 04:03 PM
Updated by:
Contributors