How to retrieve multiple hierarchy of record one by one with required details in REST API Explorer?

vishalj
Tera Expert

Scenario:

There are 2 table.

For table 1: After Gliding table there is 8 records which should be displayed in response body.

For table2: I need to glide that 8 record which found in table-1, again need to display all 10 records with their next hierarchy data.

 

Example: There is a table 'cmdb_hardware_product_model', after gliding there is 2 record which need to display as below with some attributes details.

Again there is table 'alm_hardware' and there record should be query with name of that 2 record which found in table 1. 

Expected output: As  per below example, the model ABC-1 should be displayed first with their attributes then their all asset details and again the model ABC-2 should be displayed with their attributes then their all asset details and so on. PFB👇

{
  "result": {
    "model": {
      "item": [
        {
          "Model name": "ABC-1",
          "Category": "Rack",
          "State": "In Production",
          "Short Description": "APC AR3100SP1 ",
          "Description": "\n<p><strong>NetShelter 
          "Asset": [
            {
              "asset": "P1000142",
              "Model name": "ABC-1",
            },
            {
              "asset": "P1000206",
              "Model name": "ABC-1",
            }
]
     "item": [
        {
          "Model name": "ABC-2",
          "Category": "Rack",
          "State": "In Production",
          "Short Description": "APC AR3100SP1 ",
          "Description": "\n<p><strong>NetShelter 
          "Asset": [
            {
              "asset": "P1000145",
              "Model name": "ABC-2",
            },
            {
              "asset": "P1000255",
              "Model name": "ABC-2",
            }
]

and so on

 

 

I tried with below script but the hierarchy is not properly displayed:

(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {

var array = [];	
var array2 = [];
var array3 = [];	

//...........
var gr3 = new GlideRecord('cmdb_hardware_product_model');
gr3.addEncodedQuery('manufacturer=9be9bf333723100044e0bfc8bcbe5ddf');
gr3.query();
while(gr3.next()){
var name = array2.push(gr3.display_name.getDisplayValue());	
var x = array2.toString();
}
//...........
var gr2 = new GlideRecord('alm_hardware');
gr2.addEncodedQuery('model.display_nameIN'+x);
gr2.query();
while(gr2.next()){	
var obj2 = {
        "asset": gr2.getValue('asset_tag'),
        "Model name": gr2.model.getDisplayValue(),

};
	array3.push(obj2);
//................................................	
	
var gr = new GlideRecord('cmdb_hardware_product_model');
gr.addEncodedQuery('manufacturer=9be9bf333723100044e0bfc8bcbe5ddf');
gr.query();
while(gr.next()){
	
var obj = {
        "Number": gr.getValue('display_name'),
        "Category": gr.cmdb_model_category.getDisplayValue(),
        "State": gr.status.getDisplayValue(),
	"Short Description": gr.short_description.getDisplayValue(),
	"Description": gr.description.getDisplayValue(),
	"Asset": array3
};

	
//...............................................
		
 array.push(obj);
	
}
}	
	
var str =  JSON.stringify(array);
var array = JSON.parse(str);
	
var str2 =  JSON.stringify(array3);
var array3 = JSON.parse(str2);
	
	
return {
	"model":{
		"item":array

	}
};
	
})(request, response);

 

 

Can anyone help in this? @Ankur Bawiskar @Allen Andreas 😊

0 REPLIES 0