Is there a way to get all the values from a Glide Record

Roger1
Tera Contributor

I have a need to return all the values from a reference column.  Here is my code:

var startGR = new GlideRecord('some_table');
startGR.addQuery('some_column', 'some value');
startGR.query();
if (startGR.next()){
  var refRecord = startGR.some_ref.getRefRecord();
  gs.debug (JSON.stringify(refRecord));
}

When I look at the output I have something like this:

{
  "sys_id": {},
  "prop1": {},
  "prop2": {}
}

It appears as though, I'm only getting the structure.  Is there anyway to get something like this:

{
  "sys_id": "3f86aedd47517190fwer8f0bd436d4358",
  "prop1": "car",
  "prop2": {
    "sys_id": "5t86aedd475171werwebd436eerd4358",
    "name": "green"
  }
}

or just this:

{
  "sys_id": "3f86aedd47517190fwer8f0bd436d4358",
  "prop1": "car",
  "prop2": {}
}

Without having to manually add each property.

1 ACCEPTED SOLUTION

-O-
Kilo Patron
Kilo Patron

Running

 

var activeFieldNames = global.j2js(GlideTableDescriptor('incident').getActiveFieldNames()),
	records = new global.GlideQuery('incident')
	.get('01d50b4287e7211007eb63573cbb359a', activeFieldNames);

gs.debug('\n\n' + JSON.stringify(records, null, '\t'));

 

in Scripts - Background prints

 

{
	"_value": {
		"sys_id": "01d50b4287e7211007eb63573cbb359a",
		"actions_taken": "",
		"active": true,
		"activity_due": "",
		"additional_assignee_list": null,
		"approval": "not requested",
		"approval_history": "",
		"approval_set": null,
		"assigned_to": null,
		"assignment_group": "dbe8cbc4878b5d10158298683cbb35aa",
		...
		"work_end": null,
		"work_notes": "",
		"work_notes_list": null,
		"work_start": null
	},
	"_lazyValueFetched": false
}

 

Though GlideTableDescriptor is not accessible from private scopes, so you would need to create a Script Include accessible from all scopes to include the script above - if this is needed in a private scope.

That or create from scratch a Script Include to provide the same functionality: loading active fields for a table.

View solution in original post

6 REPLIES 6

Roger1
Tera Contributor

In most frameworks, when you query the database, the object returned will have properties for all the columns in the database.  These frameworks also have to option to load the data immediately or when needed (lazy loading).  How does this work in ServiceNow?

-O-
Kilo Patron
Kilo Patron

Running

 

var activeFieldNames = global.j2js(GlideTableDescriptor('incident').getActiveFieldNames()),
	records = new global.GlideQuery('incident')
	.get('01d50b4287e7211007eb63573cbb359a', activeFieldNames);

gs.debug('\n\n' + JSON.stringify(records, null, '\t'));

 

in Scripts - Background prints

 

{
	"_value": {
		"sys_id": "01d50b4287e7211007eb63573cbb359a",
		"actions_taken": "",
		"active": true,
		"activity_due": "",
		"additional_assignee_list": null,
		"approval": "not requested",
		"approval_history": "",
		"approval_set": null,
		"assigned_to": null,
		"assignment_group": "dbe8cbc4878b5d10158298683cbb35aa",
		...
		"work_end": null,
		"work_notes": "",
		"work_notes_list": null,
		"work_start": null
	},
	"_lazyValueFetched": false
}

 

Though GlideTableDescriptor is not accessible from private scopes, so you would need to create a Script Include accessible from all scopes to include the script above - if this is needed in a private scope.

That or create from scratch a Script Include to provide the same functionality: loading active fields for a table.