Saurabh Gupta
Kilo Patron
Kilo Patron

Introduction:

Are you looking for efficiently retrieving all catalog and record producer variables as a concatenated string? You're in the right place! In this blog post, I'll explain you through the process of creating a reusable custom flow action using the ServiceNow out-of-the-box Script Include, GlobalServiceCatalogUtil. This API will give both type of variables single-row as well as multi-row in one place.

Use Cases:

We need the variables as a concatenated string to be used in description/other field.
We need all the variables to be shared over email for approvers etc.

Implementation: 
Creation of custom action

We will create a custom action Get Catalog Variables String with the inputs 
1. Task Ref - Reference from task
2. Multi-Row Needed - Boolean
and one output variable as msg.

SaurabhGupta_2-1702803542050.png

 


We will be adding the JavaScript step where we will write the below script-

 

 

 

 

var vs = new global.GlobalServiceCatalogUtil().getVariablesForTask(inputs.task, inputs.ismulti);	
var msg=	"";
	for (var i=0; i < vs.length; i++) 
	{
		var isMRVS=lbl=vs[i].multi_row+"";
		var lbl=vs[i].label+"";
		var dis=vs[i].display_value+"";
		var visible=vs[i].visible_summary+"";
		if(isMRVS=='true')
		{
			var vArr=vs[i];
			msg+= lbl +"\n";
			for (var j=0; j < vArr.table_variable.length; j++) 
			{
				msg+= "Row: " + (j+1)  + "\n"; 
				var tblArr=vArr.table_variable[j];
				for (var k=0; k < tblArr.length; k++) 
				{
					var mrvslbl=tblArr[k].label+"";
					var mrvsdis=tblArr[k].display_value+"";
					if(mrvslbl != '' && mrvsdis!='' && mrvsdis!='false') 
					{
						msg+="\t"+ mrvslbl +  ": " + mrvsdis + "\n"; 
					}
				}
				msg+="\n";
			}
		}
		else
		{
			if (lbl != '' && dis!='' && dis!='false' && visible=='true') {
				msg+= lbl + ": " + dis + "\n"; 
			}
		}

	}
	outputs.msg=msg;

 

 

 

 

 

SaurabhGupta_1-1702803103728.png

Output variables: 

SaurabhGupta_3-1702803662444.png

Flow action output:

SaurabhGupta_4-1702803706017.png

Setting script step output to the flow action output:

SaurabhGupta_5-1702803769732.png

 

Custom action testing

 

SaurabhGupta_6-1702803913601.png


Final test output:

SaurabhGupta_7-1702804070753.png

For mail script (on RITM table)

(function runMailScript(/* GlideRecord */ current, /* TemplatePrinter */ template,
										   /* Optional EmailOutbound */ email, /* Optional GlideRecord */ email_action,
										   /* Optional GlideRecord */ event) {

	var vs = new GlobalServiceCatalogUtil().getVariablesForTask(current, true);		//current will be the RITM gliderecord, if notification is on approver table with source as RITM table ===> current.sysapproval.getRefRecord()
	for (var i=0; i < vs.length; i++) 
	{
		var isMRVS=lbl=vs[i].multi_row+"";
		var lbl=vs[i].label+"";
		var dis=vs[i].display_value+"";
		var visible=vs[i].visible_summary+"";
		if(isMRVS=='true')
		{
			var vArr=vs[i];
			template.print("<div>"  + "<strong>" + lbl + "</strong></div><hr>");
			for (var j=0; j < vArr.table_variable.length; j++) 
			{
				template.print("<div><strong>Row: </strong>" + (j+1)  + "</div>"); 
				var tblArr=vArr.table_variable[j];
				for (var k=0; k < tblArr.length; k++) 
				{
					var mrvslbl=tblArr[k].label+"";
					var mrvsdis=tblArr[k].display_value+"";
					if(mrvslbl != '' && mrvsdis!='' && mrvsdis!='false') 
					{
						template.print("<div>"  + "<strong>" + mrvslbl + "</strong>" + ": " + mrvsdis + "</div>"); 
					}
				}
				template.print("<hr>");
			}
		}
		else
		{
			if (lbl != '' && dis!='' && dis!='false' && visible=='true') {
				template.print("<div>"  + "<strong>" + lbl + "</strong>" + ": " + dis + "</div>"); 
			}
		}

	}
})(current, template, email, email_action, event);

 

 

C

If you found value in this blog/article, I'd be grateful if you could either bookmark it or mark it as helpful.