GET operation via REST API - Can I return reference field not sys_user GUID or display value?

chrisfowells
Kilo Contributor

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.

5 REPLIES 5

Hitoshi Ozawa
Giga Sage
Giga Sage

To get structured JSON response from one API call, it is necessary to create a Scripted REST API.

https://docs.servicenow.com/bundle/paris-application-development/page/integrate/custom-web-services/...

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"
    }
  ]
}