GlideRecordUtil - Global

A utility class for working with GlideRecords

The GlideRecordUtil class is available in server-side scripts.

GlideRecordUtil - getCIGR(String sys_id)

Returns a GlideRecord instance positioned to the given CI sys_id, and of the right class (table).

Table 1. Parameters
Name Type Description
sys_id String The sys_id of the desired CI.
Table 2. Returns
Type Description
GlideRecord A GlideRecord instance positioned to the given CI sys_id, and of the right class (table).

var gr = new GlideRecordUtil().getCIGR("2dfd7c8437201000deeabfc8bcbe5d56");

GlideRecordUtil - getFields(GlideRecord gr)

Returns a list of all the fields in the specified GlideRecord.

Table 3. Parameters
Name Type Description
gr GlideRecord A GlideRecord instance positioned to a valid record.
Table 4. Returns
Type Description
String An array of field names for the specified GlideRecord.

var queryString = "priority=1^ORpriority=2";
var gr = new GlideRecord('incident');
gr.addEncodedQuery(queryString);
gr.query();
gr.next());

var gRU = new GlideRecordUtil();
var fieldList = gRU.getFields(gr);
gs.info(fieldList);
Output: Line breaks added for presentation.
sys_id,skills,closed_by,assigned_to,contract,category,escalation,state,reassignment_count,location,
time_worked,order,due_date,number,upon_approval,sys_tags,sla_due,follow_up,reopen_count,notify,business_stc,
caused_by,rejection_goto,assignment_group,comments_and_work_notes,incident_state,opened_at,parent_incident,
business_service,wf_activity,calendar_duration,group_list,caller_id,comments,priority,sys_updated_by,
variables,delivery_task,resolved_at,sys_updated_on,parent,active,opened_by,expected_start,watch_list,
company,upon_reject,work_notes,sys_created_by,additional_assignee_list,approval_set,cmdb_ci,user_input,
sys_created_on,close_code,contact_type,resolved_by,rfc,approval_history,activity_due,severity,child_incidents,
,subcategory,work_end,closed_at,close_notes,variables,business_duration,knowledge,approval,sys_domain_path,
sys_mod_count,problem_id,calendar_stc,work_start,sys_domain,correlation_id,sys_class_name,short_description,
impact,description,correlation_display,urgency,made_sla,delivery_plan,work_notes_list

GlideRecordUtil - getGR(String baseTable, String sys_id)

Returns a GlideRecord instance for the given table, positioned to the given sys_id, and of the right class (table).

This method is useful when you need to load a GlideRecord from a sys_id, but you don't know what the actual table is (because it may be extended from the base table). This method always returns a GlideRecord of the correct type base_table: the name of the base table that the specified sys_id is in.

Table 5. Parameters
Name Type Description
baseTable String The name of the base table containing the sys_id.
sys_id String The sys_id of the desired record.
Table 6. Returns
Type Description
GlideRecord The GlideRecord for the specified sys_id.

var gr = new GlideRecordUtil().getGR("cmdb_ci_computer", "2dfd7c8437201000deeabfc8bcbe5d56");

GlideRecordUtil - getTables(String tableName)

Returns a Java ArrayList of the ancestors of the given table name.

For example, given cmdb_ci_linux_server, this would return cmdb_ci, cmdb_ci_computer, cmdb_ci_server, and cmdb_ci_linux_server.

Table 7. Parameters
Name Type Description
tableName String Name of the table
Table 8. Returns
Type Description
ArrayList A list of ancestors of the specified table

var tables = new GlideRecordUtil().getTables("cmdb_ci_linux_server");
gs.log(tables.join(","));  
// The result would be "cmdb_ci, cmdb_ci_computer, cmdb_ci_server, cmdb_ci_linux_server".

GlideRecordUtil - mergeToGR(Object hashMap, GlideRecord gr, Object ignore)

Sets the fields in the specified GlideRecord with the field values contained in the specified hashmap, unless that field name is in the ignore hashmap.

Table 9. Parameters
Name Type Description
hashMap Object An Object instance (being used as a hashmap), with properties named for fields and containing the fields' value.
GlideRecord GR The GlideRecord instance to receive the field values.
ignore Object An optional hashmap of field names to ignore.
Table 10. Returns
Type Description
void

This example updates a computer record's name and os fields, but does not update the sys_created_by field:

var gr = new GlideRecordUtil().getGR("cmdb_ci_computer", "2dfd7c8437201000deeabfc8bcbe5d56");   
var obj = {"name": "xyz", "os": "windows 2000", "sys_created_by", "aleck.lin"};
var ignore = {"sys_created_by": true};
new GlideRecordUtil().mergeToGR(obj, gr, ignore);
gr.update();

GlideRecordUtil - populateFromGR(Object hashMap, GlideRecord gr, Object ignore)

Populates the given hashmap from the given GlideRecord instance. Each field in the GlideRecord becomes a property in the hashmap.

Table 11. Parameters
Name Type Description
hashMap Object An object being used as a hashmap.
gr GlideRecord A GlideRecord instance positioned to a valid record.
ignore Object An optional hashmap of file names not to populate.
Table 12. Returns
Type Description
void

var objectToPopulate = {};
var gr = new GlideRecordUtil().getGR("cmdb_ci_computer", "2dfd7c8437201000deeabfc8bcbe5d56");
var ignore = {"sys_created_on": true, "sys_updated_by": true};
new GlideRecordUtil().populateFromGR(objectToPopulate, gr, ignore);
// Now the objectToPopulate contains field/value pairs from the computer GlideRecord