GET operation via REST API - Can I return reference field not sys_user GUID or display value?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-05-2017 08:02 AM
When performing a GET operation you should have the option of what field/s to return for a reference, i.e. instead of just either the GUID, or the Display Value but any field from the referenced table. For instance if you've added a reference to a user to a table from the sys_user table (e.g. adding an owner of an application to an application table) and when you retrieve the data via a Rest API, for the reference field you might not want either the sys_user GUID, or the display value, but the NT_ID.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-05-2020 04:26 PM
To get structured JSON response from one API call, it is necessary to create a Scripted REST API.
Example:
Script Include
var getUserDetails = Class.create();
getUserDetails.prototype = {
initialize: function() {},
getdetails: function(userid) {
var userInfo = [];
try {
var grUser = new GlideRecord('sys_user');
if (grUser.get(userid)) {
var grCompany = new GlideRecord('core_company');
var companyName = '';
if (grCompany.get(grUser.company)) {
companyName = grCompany.name;
}
userInfo.push({
'name': grUser.name,
'email': grUser.email,
'company': companyName
});
}
return userInfo;
} catch (e) {
gs.error("ERROR=", e);
}
},
type: 'getUserDetails'
};
Result
{
"result": [
{
"name": "Abel Tuter",
"email": "abel.tuter@example.com",
"company": "ACME South America"
}
]
}