- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
on 02-15-2023 11:22 PM
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)
- This function can be used to get the full record data
- The data will be retrieved in an Object response
- 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
mergeToGR(object, gr, ignoreFields)
- This function can be used to update the record data
- 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
I hope you like my article
Kindly, Post comments if you have any queries
Thanks,
Sai Kumar B
Community Rising Star 2023 & 2022
- 1,704 Views

- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
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();


- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Nice.
More use cases identified here: https://docs.servicenow.com/bundle/tokyo-application-development/page/script/server-scripting/concep...
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Hello @Martin Ivanov
Thank you for reading my article and providing the solution that can help community users.
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Hello @Allen Andreas
Thank you for reading my article and providing reference to additional use cases.