- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-10-2021 01:32 PM
Hi, I am fairly new to servicenow so I apologize if my question is framed poorly. I have a service catalog item that uses a variable set to define a source/destination IP and port for a users firewall request. I am using a variable set so that we can have multiple groupings (rows) in the request. I am having this request trigger a workflow that will create an SCTask and populate it with the variable names/values from the variable set. I've poked around and can see how to populate the data when using variables but now when using variable sets.
Using run script workflow task how would I script it out to have it create and populate an SCtask with the proper information? Thanks in advance!
Solved! Go to Solution.
- Labels:
-
Service Catalog
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-10-2021 03:34 PM
Querying variable sets is not that straight forward. You will have to dot walk. The sample script I used and the outputs are included below and on the attachments. But here is a high level breakdown:
1) Query the RITM parent item with the varaible set
2) Ensure that you include the entire variable in a variable. It can be done in this format
variablename=gliderecordqueryobjectvariable.variables.variablesetname
in my script it would be rotmanSystems = ritmGR.variables.rotman_systems;
3) Get the row count so you know how many records to loop through
4) Loop through each row with getRow. You will be able to extract the column value as you loop through.
var rotmanSystems;
var itemID='fbec74bddb752c5043713423e2961973' //RITM SysID
//Query MRVS Object - Rotman Systems
var ritmGR = new GlideRecord('sc_req_item');
if(ritmGR.get(itemID)){
rotmanSystems = ritmGR.variables.rotman_systems;
}
//Row Counts for MVRS
var rotmanSystemsRowCount=rotmanSystems.getRowCount();//Total Rotman System Count (includes Salesforce)
//Iterate through the row records
for (var i=0;i<rotmanSystemsRowCount;i++){
var row=rotmanSystems.getRow(i);//Get Rotman System MVRS Row RECORD
var systemName=row.system_name.toString(); // Get value of system_name column
gs.print(systemName);
}
Please note the above example only shows how to query variable set values for each row. From what I understand querying is the roadblock you ran into.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-17-2021 10:07 AM
This worked for me. I was able to grab all columns and rows. Thanks!!!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-10-2021 05:15 PM
Since you mentioned you were new to ServiceNow, I really feel like I need to ask if you've tried using the out of box Service Request structure, and if so what was the roadblock that lead you down the dark path you are on - what is the need for all this querying of variables? Let's back up a step to be sure we're clear on the basics. You have a Catalog Item where you've defined variables, and if you plan on re-using any variables you can instead put them inside of a single row variable set, then add that variable set to the catalog item. Variables inside and outside of a single row variable set are treated the same when it comes to Catalog Client Scripts, Catalog UI Policies, workflow activities, and pretty much everything else. The other type of variable set is a multi-row variable set (MRVS) where you can define one or more variables, then populate it by adding any number of rows where some or all of those variables are populated.
So a user selects a Catalog Item and the request form is presented, where they populate some or all of the variables, and let's say they add a few rows to a MRVS. When they submit the request form, or add to cart and checkout, or whatever a REQ is created, which OOTB has a workflow that runs on it for some auto-approval (of the REQ) purpose, then also an RITM (one for each item in the cart) is created with the REQ listed in its Request field, so these are tied together. The RITM shows the same variables (unless you hide them) and their values as were on the request form. It's important to note that the variables are related to the RITM, but they don't exist as part of the RITM-only record like the fields (Number, Assignment group, Assigned to,...). It is also advisable to make the variables read only on the RITM to maintain their integrity - you'll know the values entered were provided by the requestor, or in many cases supplied or updated by task processors working the SCTASK(s). You can do this with a very simple onLoad Client Script on the sc_req_item table
function onLoad() {
g_form.setVariablesReadOnly(true);
}
Next you can have a workflow that runs (on the sc_req_item table) when the RITM is created. The workflow is a great place to handle any approvals, run scripts that can do all sorts of neat stuff, and create any needed Catalog Tasks (sctask) records, whenever and however you want them - sequential, parallel, in stages, none at all,... The easiest and best way to create Catalog Tasks is with a Catalog Task activity. Here you can define the Task Name / Short description, which group and/or user it should be assigned to, even some advanced scripting of these values if needed, AND which variables (including those in both kinds of variable sets) you want to appear on each task. When the time comes for the task to get created, you'll see that the RITM is listed in the Request Item field on the task, so that's all tidy, and the variables that you selected to appear show much the same that they did on the RITM, and with the same values. This is because the variables are also related to any sctasks that are created under an RITM, but they don't exist as part of the sctask-only record like the fields Number, State, Description,...
So the very same variables that you define within a Catalog Item, can appear on any or all of: the 1) request form, 2) RITM, and 3) any and all SCTASK(s). Whenever the value of a variable is populated or changed in one of these places, you will see the same value every other time you see that variable. There are plenty of times in a workflow Run Script when you need to get the value of a variable, and I've done some really cool things with getting, manipulating, and setting MRVS values, but this goes well beyond the basics, so I wanted to be sure we had that down first.
Since it sounds like we're dealing with a MRVS, if you need your task processors to see the contents of each row on a separate task or something like that we can do that with a script, or if you just need the task to show the populated MRVS, then maybe they complete one additional column to show that that row has been dealt with, or some other variable that's only shown on the task, then that's out of box Catalog Task behavior - no scripting required. With all this in mind (sorry if it was all for naught) what do you need to see on your Catalog Tasks that is not happening when they are created via the workflow activity?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-15-2021 12:37 PM
Good long read. I appreciate the information. The process you described (req > RITM creation and variables) is something I was aware of already through some research I did while working on this workflow. The reason I chose to use MRVS is to group multiple FW requests per row. I want to give the requestor the ability to request 1 firewall rule or multiple rules. Attached is an example of what the request form would look like. This would be 3 separate firewall rules based off source, destination, port.
As for how i'd like it to look in the SC_TASK would be a description of the logical groupings of fw rules. Haven't fully decided how I want it formatted but something like below:
FW request 1:
source column | Dest column | Ports
FW request 2:
source column | Dest column | Ports
etc.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-16-2021 03:46 AM
To create one Catalog Task (sc_task table record) for each row of the MRVS, without knowing that there will always be x number of rows, or at most y number of rows, you will want a workflow script. James' response on this post does a great job of explaining and detailing what you need to do.