Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

Brad Tilton
ServiceNow Employee

One of the things I came across when prepping for my Jakarta blog post was that we've released a new field type called Name-Value Pairs. Intrigued by the name I looked into it and found this description from the field types article on the docs site:

Field that maps text values. Each mapping is one-to-one, however a single Name-Value Pairs field can contain multiple mappings. Each mapping must use a unique name, and the name cannot be empty.

From here I fired up a dev instance and created a scoped app (that's just how I roll), created a new table called Data, and added a name-value pair field called Data values. This is what the default record looks like:

Screen Shot 2017-09-19 at 9.48.18 AM.png

Adding values is very straightforward.

Screen Shot 2017-09-19 at 10.06.39 AM.png

It's nice to be able to store data like this, but I think the real value comes in when you start working with it from a script. For example, here is a script I can run against the record created above.

//grab the gliderecord object for the record in the screenshot

var gr = new GlideRecord('x_85636_name_value_data');

if (gr.get('a49e63a56f110300d8c252b10b3ee41e')) {

      //iterate over the current properties in the data values field and print the values

      for (var name in gr.data_values) {

              gs.info(name + " = " + gr.data_values[name]);

      }

      //add another property to the data values field

      gs.info("Adding a height property")

      gr.data_values.height = '50';

      gr.update();

      //iterate over the current properties in the data values field and print the values

      for (var name in gr.data_values) {

              gs.info(name + " = " + gr.data_values[name]);

      }

}

Here are the printed values:

x_85636_name_value: color = Blue

x_85636_name_value: length = 27

x_85636_name_value: width = 10

x_85636_name_value: Adding a height property

x_85636_name_value: color = Blue

x_85636_name_value: length = 27

x_85636_name_value: width = 10

x_85636_name_value: height = 50

And this is what the record looks like after I've added the height property to the field from the script:

Screen Shot 2017-09-19 at 10.10.58 AM.png

The docs article points out that this field type would be useful for holding header information for a web service request where the name of each mapping is the header such as Content-Type and the value is the header value, such as Application/json. I would expect that there are a lot of other good use cases out there for this type of field, where you need to store some data that is less structured and you don't want to create a field for every possible option.

If you can think of a use case where this field type could help, please share it in the comments below.

14 Comments