How to add mappings to name value pairs field

Joe83
Kilo Contributor

Hi ServiceNow Community Developers,

I am using a name value par field for the first time and I am trying to populate the name and value using the script that ServiceNow provided as an example here

https://docs.servicenow.com/bundle/paris-platform-administration/page/administer/field-administratio...

I am however running into an issue and I am not sure what is it that I am doing wrong. There are things that I am noticing that don’t look right to me with the provided script that I am using as my model here. The provided script has the initialize() function which to me says that we are going to create a record somewhere however I don’t see the insert() function in the provided script. I am not sure if I am missing something but would you please take a look ad advise.

Here is my script https://ven03962.service-now.com/nav_to.do?uri=sys_script_client.do?sys_id=5f7f41e21b0060102d0f99ba2... and it’s a very simple copy of the one the vendor provided above. It runs on loading of a change record.

Thanks,

John

1 ACCEPTED SOLUTION

So maybe if we take a step back and ask "what requirement are you solving for with this solution"? What is it you want or need to be able to do, onload of a change record, that you has you looking at this particular solution?

I can't say that I see anything actually wrong with your script, but as I mention, the values you are creating would only exist until the end of the onload scripts scope. You're not creating any lasting information using this method. To break it down: 

onload script starts:

var gr = new.... you are creating an instance of a class

gr.initialize()... you are calling a function of the class that the memory shell of a blank record on the table. A ghost if you will.

gr.u_xlr_variables.name1..... on thing to note here is that u_xlr_variables needs to exist on the table or this would do nothing - but here you are assigning a value to your ghost record. 

onload script ends: - since you are not writing to the database, all values in this script evaporate. 

So, in essence, your script is not producing a tangible result. 

Hope this helps!

If this was helpful or correct, please be kind and click appropriately!

Michael Jones - Proud member of the CloudPires Team!

 

 

I hope this helps!
Michael D. Jones
Proud member of the GlideFast Consulting Team!

View solution in original post

7 REPLIES 7

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

please share your script here

Regards
Ankur

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

Hi Ankur,

Sorry about that, here is my script

find_real_file.png

 

Thanks,

Johannes

Hi,

name-value pair field stores value in json format

1) GlideRecord is not recommended in client script

But just for your understanding your script should look like this

var gr = new GlideRecord('change_request');

gr.initialize();

var jsonObj = {

"name1":"value1";

"name2":"value2"

};

gr.u_xlr_variable = JSON.stringify(jsonObj);

gr.insert();

Regards
Ankur

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

Michael Jones -
Giga Sage

So, name/value pairs are simply part of any javascript object. This example shows how you can add new name/value pairs to an object and later access those values. The reason they are using gr.initialize() is to instantiate the object. You are correct, they are not actually creating a record in this case, which is why they don't call gr.insert(); all of the data lives, and dies, within the scope of the script and is never written to the database. 

I'm not able to access your example, so I'm not entirely sure what you're trying to accomplish but, in essence you can use this type of functionality to "tack on" additional values to an existing object while that object exists in your script (perhaps to hold some temporary values or calculated values) but once your script ends that data will be lost. The only way to save that data would be to write it to fields that exist on the record being created, for example, and those would need to already exist; this would not let you create fields on the fly. 

This is, basically, how dot-walking works. The values of the fields are written to the object using name/value pairs, which you can then access. That's why, one a business rule for incident, you can get the number with current.number. Current is the object, number is name, and the actual number is the value. 

In the same vein, if you were in a business rule and used a line like current.number.myvalue = 'Hello'; then later any call to current.number.myvalue would return "Hello" - while that object exists in memory. 

Hope this helps!

If this was helpful or correct, please be kind and click appropriately!

Michael Jones - Proud member of the CloudPires Team!

 

I hope this helps!
Michael D. Jones
Proud member of the GlideFast Consulting Team!