Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

How can I dynamically set the name of both the name and value for the field type name-value pair?

pooja123
Tera Contributor

How can I dynamically set the name of both the name and value for the field type name-value pair?

1 ACCEPTED SOLUTION

@pooja123 

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

 

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

8 REPLIES 8

@pooja123 

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

 

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

This worked. Thanks!

You are welcome

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Rishabh Jha
Mega Guru

Hi @pooja123 ,

 

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/)