Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

I want to get the sys_id of the variable when GlideappVariablePoolQuestionSet(); used

Community Alums
Not applicable

var emptyVariables = [];

var readonlyVariables = [];
var keys = [];
var set = new GlideappVariablePoolQuestionSet();
set.setRequestID(current.request_item);
set.load();
var vs = set.getFlatQuestions();
 
for (var i = 0; i < vs.size(); i++) {
var sDisplVal = vs.get(i).getDisplayValue().toString();//--Display Value
var sLabel = vs.get(i).getLabel().toString();//-- label text
var sField = vs.get(i).getName().toString();//--variable name (e.g. requested_for) }

With this code can I get the sys_id of the particular variable?
3 REPLIES 3

Aniket Chavan
Tera Sage
Tera Sage

Hello @Community Alums ,

Please give a try to the script below and see how it works for you.

var variableName = "your_variable_name"; // replace with the actual variable name
var variableGR = new GlideRecord('sc_variable');
variableGR.addQuery('name', variableName);
variableGR.query();

if (variableGR.next()) {
    var variableSysId = variableGR.sys_id.toString();
    gs.info("Sys_ID of variable " + variableName + ": " + variableSysId);
} else {
    gs.info("Variable not found: " + variableName);
}

 

Let me know your views on this and Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks,

Aniket

Community Alums
Not applicable

@Aniket Chavan Thanks for the reply but what if more than one variable present with same name?

Hello @Community Alums ,

You can reference the catalog item name in the query, as each catalog item typically has unique variable names. This ensures there won't be any ambiguity or confusion, making your queries more precise and reliable.

var variableName = "your_variable_name"; // replace with the actual variable name
var catalogItemName = "your_catalog_item_name"; // replace with the actual catalog item name

var variableGR = new GlideRecord('item_option_new');
variableGR.addQuery('name', variableName);
variableGR.addQuery('cat_item.name', catalogItemName);
// Add more conditions if needed, such as 'type', 'widget', etc.

variableGR.query();

while (variableGR.next()) {
    var variableSysId = variableGR.sys_id.toString();
    gs.info("Sys_ID of variable " + variableName + " in catalog item " + catalogItemName + ": " + variableSysId);
}