I want to get the sys_id of the variable when GlideappVariablePoolQuestionSet(); used
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-29-2024 03:38 AM
var emptyVariables = [];
var readonlyVariables = [];With this code can I get the sys_id of the particular variable?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-29-2024 11:18 PM
Hi @pandeyved
The code snippet you've provided uses the `GlideappVariablePoolQuestionSet` class to load and iterate through the variables associated with a requested item (`sc_req_item`). However, the code as it stands does not retrieve the `sys_id` of the variables.
To get the `sys_id` of each variable, you would need to access the `sys_id` property of the variable question object. Unfortunately, the `GlideappVariablePoolQuestionSet` class does not provide a direct method to get the `sys_id` of a variable. Instead, you would need to use the GlideRecord API to query the `item_option_new` table (which stores the question definitions) to find the `sys_id` based on the variable name.
Here's an example of how you could modify the loop to get the `sys_id` of each variable:
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 question = vs.get(i);
var sDisplVal = question.getDisplayValue().toString(); // Display Value
var sLabel = question.getLabel().toString(); // label text
var sField = question.getName().toString(); // variable name (e.g. requested_for)
// Now, let's get the sys_id of the variable
var gr = new GlideRecord('item_option_new');
gr.addQuery('name', sField);
gr.query();
while (gr.next()) {
// If there are multiple variables with the same name, this will loop through all of them
var variableSysId = gr.sys_id.toString();
// Do something with the sys_id, like pushing it into an array or processing it further
// For example:
keys.push({
label: sLabel,
displayValue: sDisplVal,
variableName: sField,
sysId: variableSysId
});
}
}
// Now keys array will have objects with label, displayValue, variableName, and sysId for each variable
Please note that if there are multiple variables with the same name, the above code will retrieve all of their `sys_id`s. If you need to distinguish between variables with the same name, you may need additional logic to differentiate them, possibly based on other attributes or the context in which they are used.
Keep in mind that querying the `item_option_new` table for each variable within a loop can be performance-intensive, especially if there are many variables. It's generally a good practice to minimize the number of queries within loops. If performance becomes an issue, consider optimizing the code by collecting all variable names first and then performing a single query to retrieve all corresponding `sys_id`s.
Please mark this response as correct or helpful if it assisted you with your question.