Fetch reference field as a displayed name REST API

Sven Olsen
Tera Contributor

Hi,

 

I'm trying to fetch data from another dev instance. My question is how to fetch display name model and model category (both are reference field) in asset table. Model and model category remains blank. Both sys_id's between stances are different so I guess I have to use displayed name.

I tried getDisplayName() but it doesn't work. 

 

try {
    var r = new sn_ws.RESTMessageV2('REST API tests', 'Get Assets');

    var response = r.execute();
    var responseBody = response.getBody();
    var httpStatus = response.getStatusCode();
	
    var parsedData = JSON.parse(responseBody);	
    for (i = 0; i < parsedData.result.length; i++) {
		
        var assets = new GlideRecord('alm_asset');
        assets.initialize();
        assets.display_name = parsedData.result[i].display_name;
        assets.model = parsedData.result[i].model;
        assets.model_category = parsedData.result[i].model_category;
        assets.asset_tag = parsedData.result[i].asset_tag;
		assets.insert();
    }
} catch (ex) {
    var message = ex.message;
}
1 ACCEPTED SOLUTION

Hi,

You are using table API and to get display value in response you need too update your links like below:

 

 

https://devxxxxx.service-now.com/api/now/table/cmdb_model_category/c9d5bc50c3031000b959fd251eba8fbc?sysparm_display_value=all

and 

https://devxxxxx.service-now.com/api/now/table/cmdb_model/12345bc50c3031000b959fd251eba8fbc?sysparm_display_value=all

 

Add additional query parameter: 

sysparm_display_value=all

 

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

View solution in original post

3 REPLIES 3

Anil Lande
Kilo Patron

Hi,

Have you tried printing response object? Are you receiving display Value from API?

If you are receiving only sys_id then you need to change Scripted Rest API defination in another instance and return display value instead of sys_id (or return both values). Then you can get that value in your current instance.

Can you please share which API you are using to get data? OOB or custom?

 

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

model and model category are represented in Json as a link and sysid

I'm using REST message to fetch data between two my pdi's

 

"model_category": {
            "link": "https://dev*****service-now.com/api/now/table/cmdb_model_category/81feb9c137101000deeabfc8bcbe5dc4",
            "value": "81feb9c137101000deeabfc8bcbe5dc4"
 
  "model": {
            "link": "https://dev*****.service-now.com/api/now/table/cmdb_model/f8fa15df0a0a0b9100a14bd778fb212a",
            "value": "f8fa15df0a0a0b9100a14bd778fb212a"
         },

 

 

Hi,

You are using table API and to get display value in response you need too update your links like below:

 

 

https://devxxxxx.service-now.com/api/now/table/cmdb_model_category/c9d5bc50c3031000b959fd251eba8fbc?sysparm_display_value=all

and 

https://devxxxxx.service-now.com/api/now/table/cmdb_model/12345bc50c3031000b959fd251eba8fbc?sysparm_display_value=all

 

Add additional query parameter: 

sysparm_display_value=all

 

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande