- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-11-2020 11:32 AM
How can I dynamically set the name of both the name and value for the field type name-value pair?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-12-2020 12:51 AM
please try this updated script
Few corrections you should use
1) you should declare the object outside
2) set the value after the loop
3) use toString() while assigning value to obj
var obj = {}; // declared outside
// Query RITM from Approval
var varown = new GlideRecord('sc_item_option_mtom');
varown.addQuery("request_item", current.request_item);
varown.orderBy("sc_item_option.order");
varown.query();
// Go through all the variables
while (varown.next()) {
var varType = varown.sc_item_option.item_option_new.type;
var visible = varown.sc_item_option.item_option_new.visible_summary;
var question = GlideappAbstractChoiceListQuestion.getQuestion(varown.sc_item_option.item_option_new);
question.setValue(varown.sc_item_option.value);
// Do not display if variable type is container start/end/split
if (varType != '19' && varType != '20' && varType != '24') {
// If the variable lable is empty, or the display value, or summary is not true do not display the field
if (question.getLabel() != "" && question.getDisplayValue() != "" && visible == true) {
var p = question.getLabel();
var pt = question.getDisplayValue();
obj[p] = pt.toString(); // used toString()
}
}
}
current.setValue("u_task_variables", JSON.stringify(obj)); // set value outside
Mark the comment as a correct answer and helpful if this answers your question.
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-12-2020 12:51 AM
please try this updated script
Few corrections you should use
1) you should declare the object outside
2) set the value after the loop
3) use toString() while assigning value to obj
var obj = {}; // declared outside
// Query RITM from Approval
var varown = new GlideRecord('sc_item_option_mtom');
varown.addQuery("request_item", current.request_item);
varown.orderBy("sc_item_option.order");
varown.query();
// Go through all the variables
while (varown.next()) {
var varType = varown.sc_item_option.item_option_new.type;
var visible = varown.sc_item_option.item_option_new.visible_summary;
var question = GlideappAbstractChoiceListQuestion.getQuestion(varown.sc_item_option.item_option_new);
question.setValue(varown.sc_item_option.value);
// Do not display if variable type is container start/end/split
if (varType != '19' && varType != '20' && varType != '24') {
// If the variable lable is empty, or the display value, or summary is not true do not display the field
if (question.getLabel() != "" && question.getDisplayValue() != "" && visible == true) {
var p = question.getLabel();
var pt = question.getDisplayValue();
obj[p] = pt.toString(); // used toString()
}
}
}
current.setValue("u_task_variables", JSON.stringify(obj)); // set value outside
Mark the comment as a correct answer and helpful if this answers your question.
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-12-2020 01:13 AM
This worked. Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-12-2020 02:52 AM
You are welcome
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-12-2020 12:30 AM
Hi
The below snippet should work:
// Query RITM from Approval
var varown = new GlideRecord('sc_item_option_mtom');
varown.addQuery("request_item", current.request_item);
varown.orderBy("sc_item_option.order");
varown.query();
// Go through all the variables
var task_variables = {};
while (varown.next()) {
var varType = varown.sc_item_option.item_option_new.type;
var visible = varown.sc_item_option.item_option_new.visible_summary;
var question = GlideappAbstractChoiceListQuestion.getQuestion(varown.sc_item_option.item_option_new);
question.setValue(varown.sc_item_option.value);
// Do not display if variable type is container start/end/split
if (varType != '19' && varType != '20' && varType != '24') {
// If the variable lable is empty, or the display value, or summary is not true do not display the field
if (question.getLabel() != "" && question.getDisplayValue() != "" && visible == true) {
var p = question.getLabel();
var pt = question.getDisplayValue();
task_variables[p] = pt;
+current.setValue("u_task_variables", JSON.stringify(task_variables));
}
}
}
Thanks & Regards,
Rishabh Jha
Aavenir (https://www.aavenir.com/)
