SNOW GlideRecord to JSON

This week I needed to implement an integration with Service Now (SNOW).

The integration would fire an API request whenever a new record is inserted, or when an existing one is updated. The way to achieve this in SNOW is by creating a Business Rule, with custom script in the “Advanced” section.

To give a little more background, my goal was to send the entire record, GlideRecord in SNOW, in the payload of the outbound request.

Finding code examples for the API request itself was pretty straightforward (with SNOW’s RESTMessageV2).

This is why I was surprised when the body I received in the inbound side included only the record’s keys, without their values.

The reason was - I used [JSON.stringify()]() to convert the GlideRecord to a JSON, which was sent as the payload.
It turns out, some values in GlideRecords cannot be encoded by the JSON object.

The Solution

After spending some time in SNOW developer docs and community forum, I decided to go with a naive approach.

Iterate over the GlideRecord props and add each prop with its’ value, using the getValue method, to a new JavaScript object.

function glideRecordToJson(gr) {
  var obj = {};

  for (var prop in gr) {
    if (gr[prop]){
      obj[prop] = gr.getValue(prop);
    }
  }

  return obj;
};
comments powered by Disqus