How to access Variables Set/Multi Variable Set Variables in a flow (Flow Designer)
Hi all,
With the impending advent of Flow Designer becoming the new De Facto Tool for Workflows in ServiceNow I decided to utilise the tool for use with my customers Service Catalog. But was hit by a roadbloack:
How do I access Variables from a Multi/Variable Set in my current Flow?
Interestingly there doesn't seem to be an OOTB way of accessing Variables from a Variable Set directly using the "Flow Designer support for Service Catalog".
Steps to do this for a MVS (for a Variable Set just use an object, not Array.object:
1. Create a custom Action:
Feed the custom action the RITM that you'll be using in the flow (I've been forced to feed it the RITM number/Sys ID, as for some reason unknown to me you can't feed the flow a single record, there is only multi record select).
2. Create a Script Step:
Set the input variable to the request item (number/sys ID). Now in the script you'll need to GlideRecord into the RITM process the data into an Objects Array. In the below case I have looped through a multi variable set and set the OUTPUT variable to the newly created object.Array.
(function execute(inputs, outputs) {
// ... code ...
var ritm = new GlideRecord('sc_req_item');
ritm.get(inputs.sc_req_item);
var objectArray = [];
var fullSet = ritm.variables.[YOUR VARIABLE SET NAME];
for ( var i = 0 ; i < fullSet.getRowCount() ; i++){
var item = {};
item.hostType = fullSet[i].[VARIABLE NAME 1];
item.hostName = fullSet[i].[VARIABLE NAME 2];
item.hostOperatingName = fullSet[i].[VARIABLE NAME 3];
item.networkZone = fullSet[i].[VARIABLE NAME 4];
item.dataCenter = fullSet[i].[VARIABLE NAME 5];
item.componentName = fullSet[i].[VARIABLE NAME 6];
item.applicationImplementationLanguage = fullSet[i].[VARIABLE NAME 7];
item.languageVersion = fullSet[i].[VARIABLE NAME 8];
item.applicationServer = fullSet[i].[VARIABLE NAME 9];
item.lifeCycleStage = fullSet[i].[VARIABLE NAME 10];
item.brand = fullSet[i].[VARIABLE NAME 11];
objectArray.push(item);
}
outputs.objectarray = objectArray;
})(inputs, outputs);
In the above case I've had to loop through depending on the row count, this is used in the case of a multi variable set (as the number of loops can vary from RITM to RITM).
Tip: Remember to set outputs.NAME and not outputs.LABEL (easy mistake to make).
3. Set the Script Steps Output Variables:
You'll now need to set the output variable of the Script Step to the same structure of the object.Array you've created. Currently Flow Designer supports String, Integer, Date/Time, Choice, True/False and Object as outputs in your object. See below for my setup.
4. Parsing your data back to your flow
For this step you'll need to create a new object.Array output variable and drop the script steps output variable to the subflow output variable value:
5. (Optional) Accessing data in Flow when multi variable set used.
In the case of pulling data from a multi variable set you'll have to create a forEach loop in your Flow and loop through the object.Array to access the data as you'll have a variable number of objects in that Array. In the Case of a Variable Set you can just use an object output and not an Array.object.
Congratulations! You now have access to your data!
REQUEST:
If you find the article helpful, please mark the article as helpful and do remember to bookmark this article.
If you need any further advice on this topic feel free to contact me! (No promises, but i'll do my best to help!)
https://www.servicenow.com/community/itsm-articles/how-to-access-variables-set-multi-variable-set-variables-in-a/ta-p/2310591