Storing hashmap in memory in ServiceNow

Mike278
Kilo Explorer

So let's say I have the following function as an example which is a scheduled job that runs once a day:

function buildMap(){
var hashMap = [];
hashMap["basketball"] = "ball";
hashMap["hockey"] = "stick";
hashMap["tennis"] = "racket";
}

Is there a way to store this hashMap variable above so that it can be accessed whenever I want?
Let's say this scheduled job runs at midnight once a day, but I want to have a fix script for example which

I would like to run manually, to be able to check for a specific value which was built previously by this scheduled

job. Is this possible in ServiceNow without having to rerun the scheduled job again when I want to access the

value in a hash map?

5 REPLIES 5

You just need to convert your hashmap to JSON and save the JSON as a string in your property. Here's an example I ran in a background script to illustrate how it works. Give it a try. It outputs the hashmap as an object, converts the object to a JSON string using JSON.stringify() and turns it back to the object using JSON.parse(). 

var hashMap = {};
hashMap["basketball"] = "ball";
hashMap["hockey"] = "stick";
hashMap["tennis"] = "racket";
// output the object
JSUtil.logObject(hashMap);

// convert to JSON string
// use this to save value to a property
var jstr = JSON.stringify(hashMap);
gs.print(jstr);

// convert back to object
// use this to change the string back to your hashmap so you can use it
var newObj = JSON.parse(jstr);
JSUtil.logObject(newObj);