Script for Only Visible Variables from Request Form
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-18-2023 10:39 AM - edited 01-18-2023 10:40 AM
Afternoon,
So I am testing a script and writing variables to the REQ comments when a request is submitted. This works, but it's writing variables that are hidden on the request form because they have data in them (scripts populating data in the fields in the background).
How can I update this script to only include variables that are visible on the request form?
var ritm = new GlideRecord('sc_req_item');
ritm.get('request', current.sys_id);
var reqComment;
var variables = ritm.variables.getElements();
for (var i=0;i<variables.length;i++) {
var question = variables[i].getQuestion();
var label = question.getLabel();
var value = question.getDisplayValue();
if(label != '' && value != '' && value != 'false'){
reqComment += ' ' + label + " = " + value + "<br/>";
}
}
Please mark this response as correct and/or helpful if it assisted you with your question.
Steven
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-18-2023 11:37 AM
Remember you can have more than one RITM per REQ and with a related list of RITMs at the bottom of the REQ the details of the RITM or one click away. Since variables can be displayed dynamically and different people can have different results you’ll probably have to script the fields specifically. Before you do that think about what problem are you trying to solve and if it’s worth it…remember, there can be more than one RITM and the information you’re trying to add is only one click away.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-18-2023 12:14 PM
In our environment we don't allow carts and there is only 1 RITM per REQ.
My question is can you check the visibility of a variable on a request form via a script? I've seen .isVisible() and .getDisplay(), but in a run script in a workflow, neither of those work I don't believe.
Please mark this response as correct and/or helpful if it assisted you with your question.
Steven
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-18-2023 01:20 PM
In our environment we don't allow carts
Until someone (some head-honcho) changes that and asks you to retrofit everything to work in a multi-requested-item scenario e.g. order-guides.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-18-2023 01:27 PM
My question is can you check the visibility of a variable
Short answer: no.
Longer answer: stuff that involves g_form, visibility, display, etc. is stuff that belongs to the client side. That is, stuff that works in a browser environment (while the Catalog Item is being edited).
Stuff that involves Business Rules, Workflows, Flows (except triggering the latter, which lately can be... triggered from client side) is stuff that belongs to the server side. That is, stuff that runs on ServiceNow's servers, after the Catalog Item has been submitted by the browser.
The two environments are totally separated and calls happen only from client side to server side. Server can only answer to calls from Client. Thus stuff running in a Workflow or Business Rule cannot call out to Client Side becuase
- by the time the server stuff executed, the client side sta does no longer exists
- such communication initiation is technically impossible (if it were possible, it would be the heaven of malicious users)