Sai Kumar B
Mega Sage
Mega Sage

In OOB, we have a useful utility, GlideRecordUtil() script include to 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
Output
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

 

 

Comments
Martin Ivanov
Giga Sage
Giga Sage

Good one. I can think of a possible usage of the seconde message:

var incidentGR = new GlideRecord('incident');
incidentGR.addEncodedQuery('state=1^caller_id=77ad8176731313005754660c4cf6a7de'); //David Miller

var updateData = {"caller_id": "6816f79cc0a8016401c5a33be04be441"}; //Martin Ivanov

new GlideRecordUtil().mergeToGR(updateData , incidentGR);

incidentGR.updateMultiple();
Allen Andreas
Administrator
Administrator
Sai Kumar B
Mega Sage
Mega Sage

 

Hello @Martin Ivanov 

Thank you for reading my article and providing the solution that can help community users.

 

Sai Kumar B
Mega Sage
Mega Sage

Hello @Allen Andreas 

Thank you for reading my article and providing reference to additional use cases.

 

Version history
Last update:
‎02-15-2023 11:22 PM
Updated by:
Contributors