logo

NJP

GlideRecord utility class

Import · Feb 16, 2023 · article

In OOB, we have a useful utility, GlideRecordUtil() script includeto work with GlideRecords. I've found two handy functions to fetch and update the record data.

  • populateFromGR()
  • mergeToGR()

populateFromGR(object, gr, ignoreFields)

  1. This function can be used to get the full record data
  2. The data will be retrieved in an Object response
  3. Object : Pass the object variable to store the data, gr - Pass the GlideRecord instance, ignoreFields - An optional object parameter that can exclude fields in the final response.

Sample script

var responseObject = {}; //hashmap object variable to store final response
var getData = new GlideRecordUtil().getGR("change_request", "c83c5e5347c12200e0ef563dbb9a7190"); //pass the table name and Record sys_id to get the GlideRecord instance
var ignoreFields = {"sys_created_on": true, "sys_updated_by": true}; //Pass the fields that you want to exclude in the final response
new GlideRecordUtil().populateFromGR(responseObject, getData, ignoreFields); //The main function to retrieve the data
gs.print(JSON.stringify(responseObject, null, 4)); //Decode the object

Output

SaiKumarB_0-1676529856364.png

mergeToGR(object, gr, ignoreFields)

  1. This function can be used to update the record data
  2. object - Pass the field names and values, gr - Pass the GlideRecord instance, ignoreFields - An optional object parameter to ignore the fields.

Sample script

var updateObject = {"category" : "Hardware"}; //Pass the field name and values
var updateData = new GlideRecordUtil().getGR("change_request", "c83c5e5347c12200e0ef563dbb9a7190"); //Get the GlideRecord instance
var ignoreFields = {"sys_created_on": true}; //These fields will not be updated
new GlideRecordUtil().mergeToGR(updateObject , updateData, ignoreFields); //The main function picks the object as Input
updateData.update(); //Update the record using GlideRecord instance

SaiKumarB_1-1676530550020.png

I hope you like my article

Kindly, Post comments if you have any queries

Thanks,

Sai Kumar B

Community Rising Star 2023 & 2022

View original source

https://www.servicenow.com/community/developer-articles/gliderecord-utility-class/ta-p/2480002