The CreatorCon Call for Content is officially open! Get started here.

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

joshuapicaro
Tera Contributor

I have a field of the Name-Value pair and I need to dynamically set both the name and the value.

I tried this:

var gr = new GlideRecord('custom_table');

gr.get(sys_id);

var dynamic_name = 'Name that changes';

var dynamic_value = 'Value that changes'
gr.name_value_pair_field[dynamic_name] = dynamic_value;

gr.update

 

In doing so I get an error that looks like this:

'Cannot set property "Value that changes" of undefined to "Value that changes"

3 REPLIES 3

Jace Benson
Mega Sage

The value is a stringified JSON object.

Client side this will set it and you can code in the values

g_form.setValue("u_name_value_pair",JSON.stringify({"na":"val"}));

Server side this will set it and you can code in the values

(function executeRule(current, previous /*null when async*/) {

	// Add your code here
	current.setValue("u_name_value_pair", JSON.stringify({"dynamicname":"dynamicvalue"}));
})(current, previous);

Joe83
Kilo Contributor

I believe the above script would work if you make the following change:

gr.name_value_pair_field[dynamic_name] = dynamic_value.toString();

treycarroll
Giga Guru

I noticed that the code above is missing the parentheses after update.   Should be gr.update().   gr.update will fail silently.