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

Harsh Vardhan
Giga Patron

Can you try with below example. 

 

var grIncident = new GlideRecord("incident");
grIncident.query(); 
grIncident.next();
gs.print(JSON.stringify(new GlideSPScriptable().getFieldsObject(grIncident,"caller_id"))); // add your reference field

 

Reference:

 

https://docs.servicenow.com/en-US/bundle/vancouver-api-reference/page/app-store/dev_portal/API_refer... 

 

 

Thanks for help, but I'm running the Toyko version.

try to run in background script, it will work on Tokyo as well, 

Thanks for your help, but it is not working for me.  I'm getting java NullPointerExceptions.  Also based on the documentation, it looks like it return names and not values.

java.lang.NullPointerException: org.mozilla.javascript.JavaScriptException: java.lang.NullPointerExceptio