logo

NJP

Generate key value object of the table columns and its values

Import · Jun 18, 2022 · article

Hello,

In this article, I will show you how you can take the field columns and its values dynamiclay as key/value pairs.

Here i am using incident table record.

var gr = new GlideRecord("incident");
if(gr.get("57af7aec73d423002728660c4cf6a71c")) {
var arr =[];
var obj;
  //this gives us all the column names and its value as key map
  for(key in gr) {
    obj={};
    obj["'"+key+"'"] = gr[key];
    arr.push(obj);
  }
}
//Now you can send this variable arr to any 3rd party and they can parse it with below code.
for(i=0;i<arr.length;i++) {
    for(key in arr[i])
    gs.print(key+" "+arr[i][key]);
}

Here is the output.

image

Hope this helps.

Let me know if you have any questions in the comments below.

Mark the article as helpful and bookmark if you found it useful.

View original source

https://www.servicenow.com/community/developer-articles/generate-key-value-object-of-the-table-columns-and-its-values/ta-p/2301843