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 05:09 PM
I don’t think it would be possible since you would essentially have to recreate the form. What is visible can be different for different users or values etc…
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-18-2023 01:17 PM
Create a Variable set that you add to all Catalog Items, that has a
- hidden field to hold list of variables
- an onSubmit script that updates the hidden variable with the list of visible variables
- use the value of the hidden field to obtain a list of variables (visible) that should be included in the summary.
Or implement a browser environment server side that can simulate Catalog Items and get the visible fields after running such a simulation.
In addition to @ricker's note about NOT assuming that you will always have one Requested Item for each Request, If you allow an advice, in script, whenever using <GlideRecord>.get, use an if to do whatever should be done (only) if it succeeds:
if (ritm.get('request', current.sys_id)) {
var reqComment;
var variables = ritm.variables.getElemen
...
}