Can I use g_form.setValue() in a name value pairs field?

Joe83
Kilo Contributor

Hi ServiceNow Community Developers,

Do you guys know if one can use the g_form.setValue() API in a name value pairs field? If so would you please provide examples of how one can go about in doing that. 

Thanks,

John

1 ACCEPTED SOLUTION

Hi,

you need to parse the array of json objects

Sample below

var arr = '[{"variable":"jm_template_variable","type":"xlrelease.StringVariable"},{"variable":"jm_template_var2","type":"xlrelease.StringVariable"},{"variable":"jm_date","type":"xlrelease.DateVariable"},{"variable":"jm_boolean","type":"xlrelease.BooleanVariable"},{"variable":"jm_array","type":"xlrelease.ListStringVariable"},{"variable":"jm_integer","type":"xlrelease.IntegerVariable"}]';

var parser = JSON.parse(arr);

var obj = {};

for(var i=0;i<parser.length;i++){
obj[parser[i].variable] = parser[i].type;
}

g_form.setValue('u_name_value', JSON.stringify(obj));

Output:

find_real_file.png

Regards
Ankur

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

View solution in original post

8 REPLIES 8

Ankur Bawiskar
Tera Patron
Tera Patron

@Joe 

you need to set json string using the g_form.setValue()

Regards
Ankur

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

Hi Ankur,

Thanks for your response, would you please provide an example.

For instance lets say i have a json object that has the name and the value and i want to set up the name value pair field on a form using the name and value that I have on my json object how would I go about doing that? Kindly advise please.

Thanks,

Johannes

Hi,

Something like this

var obj = {};
obj.name = 'abel';
obj.email = 'abel@example.com';

g_form.setValue('u_name_value', JSON.stringify(obj));

Output:

find_real_file.png

Regards
Ankur

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

Hi Ankur,

Thank you so much this was very helpful.

One more question please if you don't mind, let's i have a an array of json objects like 

[{"variable":"jm_template_variable","type":"xlrelease.StringVariable"},{"variable":"jm_template_var2","type":"xlrelease.StringVariable"},{"variable":"jm_date","type":"xlrelease.DateVariable"},{"variable":"jm_boolean","type":"xlrelease.BooleanVariable"},{"variable":"jm_array","type":"xlrelease.ListStringVariable"},{"variable":"jm_integer","type":"xlrelease.IntegerVariable"}]

so basically i want to dynamically set the name value pair field so that it has the 6 names and values like in my array above - 

where name property is jm_template_varaible and it's value is xlrelease.StringVariable and so on so that in the end my name value pairs field has six occurences like my json array above. Do you know if this is possible.

Thanks,

Johannes