Exploring dictionary types : Name-Value Pairs

Published Categorized as ServiceNow Tagged , ,

JSONs are widely recognised for their capability to store multiple values in a single variable. In ServiceNow, we frequently use JSONs in our scripts; however, there is also a dictionary data type available. This allows you to store a simplified version of JSON within a record in a table, organised as Name-Value pairs.

When you need to store data in a key-value pair format, rather than creating a separate many-to-many (m2m) table with just two columns, the ‘Name-Value Pairs‘ dictionary type can be a more efficient solution. A common use case for this could be storing rate cards within a task record.

Dictionary definition

Form view

Script usage

  • GlideRecord.getValue(‘u_rate_cards’) will return a string JSON like
{"Engineer":"100","Executive":"60","Analyst":"50"}
  • GlideRecord.u_rate_cards.Engineer will return ‘100’
  • GlideRecord.u_rate_cards.Analyst will return ’50’
  • and so on…

The field value can be set in a similar manner.

nv = '{"Engineer":"100","Executive":"60","Analyst":"50"}';
GlideRecord.u_rate_cards = nv;


OR


GlideRecord.u_rate_cards.Engineer = "100";
GlideRecord.u_rate_cards.Executive = "60";
GlideRecord.u_rate_cards.Analyst = "50";

Next steps

Familiarise yourself with this dictionary type and keep it in mind, as it could prove useful in the future when the need arises in your process.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses User Verification plugin to reduce spam. See how your comment data is processed.