DirkRedeker
Mega Sage

Hi

One interesting question asked in the Community was, if we can track all Workflow Activities executed on any given record in the work notes of the attached record.

 

Scenario:

You have a Workflow attached to an Incident, and you want to track each executed step of that workflow in your work notes. That way, you can always see, WHICH Workflow Activity was executed when.

Note:

If you have a need of that scenario in a similar way, you can easily modify this example, to Log all executed Workflow Activites to some other place, if you do not want to write them into your work notes.

The result should look something like shown in the screenshot below:

find_real_file.png

The Work Notes lists the executed Workflow Activities.

 

Example to implement

In my example, I will just implement a simple Workflow to run on the "incident" table.

The Workflow will just have three Timer Activities in sequence, each waiting for 15 seconds.

Follow the screenshots for reviewing the Workflow Definition below:

find_real_file.png

find_real_file.png

In the workflow, I just put three sequential "Timer" Workflow Activites, which each waits just for 15 seconds.

find_real_file.png

 

Logging the advance of the Workflow to the Incident's Work Notes

For logging the steps of the Workflow, I created a new Business Rule on the "Workflow History" [wf_history] table (which holds a log of all executed Workflow Activities per Workflow Context).

The Business Rule logs all executed Workflow Activities to the Work Notes of the Incident, where the Workflow is attached to.

Here is the Business Rule:

find_real_file.png

On the Advanced Tab of the Business Rule, I filled in the following code (COPY&PASTE version below):

find_real_file.png

(function executeRule(current, previous /*null when async*/) {
	if (current.context.table == 'incident') {
		var grInc = new GlideRecord('incident');
		var incNum = '';
		
		grInc.addQuery('sys_id', current.context.id);
		grInc.query();
		if (grInc.next()) {
			incNum = grInc.number;
			
			grInc.work_notes = 'Workflow on Table: ' + current.context.table
				+ ' / Workflow Name: ' + current.context.workflow_version.name
				+ ' / Workflow Activity executed: '	+ current.activity.name
				+ ' / Started: ' + current.started 
				+ ' / Ended: ' + current.ended
				;
			grInc.update();
		}
		
		gs.info('Workflow on Table: ' + current.context.table
				+ ' / Number: ' + incNum
				+ ' / Workflow Name: ' + current.context.workflow_version.name
				+ ' / Workflow Activity executed: '	+ current.activity.name
				+ ' / Started: ' + current.started 
				+ ' / Ended: ' + current.ended
				);
	}

})(current, previous);

 

That's it! Simple!

 

Do a Test Drive

 

Now, I created a new Incident, to fire the workflow:

find_real_file.png

After about one minute, the workflow ran through and added the following entries into the Work Notes of the attached Incident record:

find_real_file.png

That way, your incident will reflect the steps taken so far.

 

Additionally, the Business Rule wrote the information to the system log (see screenshot below):

find_real_file.png

This will give you the steps executed of each workflow on the incidents.

 

 

Thank you for reading, and I hope you enjoyed this article.

Please provide your feedback to this article in the comments below.

If you like it, just mark this article as helpful and bookmark it using the "Bookmark" button above for later access.

Have fun and built something on ServiceNow

Dirk

---------------------------------------------------------------------

If you like to also review my other articles on the ServiceNow Community, please have a look at the overview here:

Overview of my articles

NOTE: The content I provide here is based on my own experiences and does not necessarily represent my employer's views.

Version history
Last update:
‎06-03-2020 01:32 PM
Updated by: