logo

NJP

Printing off the Workflow Context During Runtime

Import · Jul 24, 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 is a quick tip on how to find out what exactly is in the context object available in a workflow. What's that you ask? What is a "context" object? Well it and the activity object are two useful objects created for every workflow by the ServiceNow platform only during run-time of the workflow. These objects are useful for a variety of reason. The most useful reason is for grabbing the names of the current context and activity to print out in the system log. Since there is no documentation on these I thought it might be useful to show how to "dump" the context object to garner what properties are available.

1. Create a new Workflow.

a. Name: CCS - Print Context Object
b. Table: Global
2. Drag out a Run Script Activity.

a. Name: Print Context Object

b. Script:

// always set up a location for logging. Note that this is useful
// no matter what Activity script you might need it in!
var location = context.name + '.' + activity.name;

// Build our complete message at once
var message = 'Context Object: \n';
for (var item in context) {
    message += item + ': ' + context[item] + '\n';
}
// print off our results in the system log
gs.info('---> [{1}] {0}', [message, location]);

image

image

3. Now run the workflow.

4. Navigate to System Logs > System Log > All and search the Message column for "--->".

Your results should look like this:

---> [CCS - Print Context Object.Print Context Object] Context Object:

sys_meta: sys_meta

ert_outlier_workflow_actions: b69c7e18eb532100ec9a82810206fea4

parent:

return_value: {}

timezone:

sys_updated_on: 2023-12-16 19:17:17

cumulated_avg_ert: false

auto_start: false

result:

sys_id: 5d0ccd5f978f355091807f200153af9a

requires_ert: true

sys_updated_by: admin

scratchpad: {}

activity_count: 2

ert_long_running_actions: 51782761ac1464174baaeb9af4b9ae76,803b3c728f320100ec9a6441f0f923ef,b69c7e18eb532100ec9a82810206fea4

sys_created_on: 2023-12-16 19:17:17

sys_domain: global

stage_state:

without_current_wf_actions: 51782761ac1464174baaeb9af4b9ae76,b69c7e18eb532100ec9a82810206fea4

id: dd0ccd5f978f355091807f200153af9a

state: executing

column_renderer: a56213111b030100adca1e094f0713ac

started_by: 6816f79cc0a8016401c5a33be04be441

table: wf_workflow_execution

sys_created_by: admin

workflow: 814bc1df978f355091807f200153afe1

sys_mod_count: 1

active: true

started: 2023-12-16 19:17:17

sys_domain_path: /

sys_tags:

after_business_rules: false

workflow_version: d22b01df978f355091807f200153af57

parent_activity:

schedule:

stage:

due:

name: CCS - Print Context Object

ended:

running_duration:

activity_index: 2

Neat stuff, eh?! Extra information when working with ANY workflow is always useful!

A perusal of the various properties is well worth the study time. Let me draw your attention to the logging statements around the center of the dump. Kind of interesting. I will address how to pull out that kind of information in a better way in my next article.

BTW, you can repeat this exercise for the activity object as well.

Enjoy!

Steven Bell.

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

image

Originally published on: 07-24-2018 12:17 PM

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

View original source

https://www.servicenow.com/community/developer-articles/community-code-snippet-printing-off-the-workflow-context-during/ta-p/2329838