Service Now - Get Table Columns and Labels With Script

Sunday Feb 9, 2020

Having all the column names and labels as a javascirpt object comes in handy, so here’s how we do it.

Code

Here’s the code:

  function getColumnsForTable(table){
    var gr = new GlideRecord('sys_dictionary');
  	var cols = {};
    gr.addQuery('name', table);
    gr.query();
    while(gr.next()){
     if(gr.element.toString().length > 0){
       var obj = {};
       cols[gr.element.toString()] = gr.column_label.toString()
     }
   }
     return cols
  }
 

It returns and object with all columns and labels:

cols[column_name] = "Column Label";