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

asifnoor
Kilo Patron

Hello Pooja,

You need to elaborate your question more. 

In general, refer to this link for name value pairs.

https://docs.servicenow.com/bundle/orlando-platform-administration/page/administer/field-administrat...

Mark the comment as a correct answer and helpful if this answers your question.

I have created name-value pair field on catalog task table. Using business rule I want copy variables details from sc_item_options table into this field. Using below script I am able to set value but name is not setting dynamically.

Name is taking as p. I want to know how we can set field with dynamic name and values.

var p = question.getLabel();
var pt = question.getDisplayValue();

current.setValue("u_task_variables",JSON.stringify({p:pt}));

 

Let me know if more information required.

 

Thanks,

Pooja

 

 

Hi Pooja,

I assume this is through some BR

Also share what type of BR it is? complete script

Can you try this

var p = question.getLabel();
var pt = question.getDisplayValue();

var obj = {};

obj['p'] = pt.toString();

current.setValue("u_task_variables",JSON.stringify(obj));

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

BR is before insert on catalog task table. I tried after insert as well. Above script is working but it is setting only one variable. 

Have attached result screenshot.

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

var obj = {};

obj[p] = pt;

+current.setValue("u_task_variables", JSON.stringify(obj, pt));


}
}
}