Json value printing as undefined.

AnandKumar1
Tera Expert

HI Team,

I am retrieving the values from MRVS and storing the value into array. However, I am receiving undefined value. please assist anything I am missing here.

 

var gr = new GlideRecord("sc_req_item");
gr.addQuery('number', "RITM********");
gr.query();
if(gr.next()){

var source_name = [];
var target_name = [];


var mvrs = gr.variables.u_request_details; // retriving the multirow variable set values.
var totalRows = mvrs.getRowCount();
gs.print(totalRows);

var sourceArr = [];
var targetArr = [];

for (var i = 0; i < totalRows; i++) {


var sourceValue = mvrs.getRow(i).getCell('source_cmdb').getCellDisplayValue();
var valueExtract = sourceValue.split(',');
sourceArr.push(valueExtract.toString());
gs.print("Source ARRAY" + sourceArr); // getting response as expected

var targetValue = mvrs.getRow(i).getCell('target_cmdb').getCellDisplayValue();
var trg = targetValue.split(',');
targetArr.push(trg.toString());
gs.print("Target ARRAY" + targetArr);  // getting response as expected

for (var k = 0; k < sourceArr.length; k++) {
var sourceCMDB = sourceArr[k].source_cmdb;

gs.print("sourcecmdb " + sourceCMDB);  // getting response as undefined

 

 // var sourceCMDB array i am going to use it in the cmdb_ci table to get the ci name and will pass into api.

 


}

for (var j = 0; j < targetArr.length; j++) {
var targetCMDB = targetArr[j].target_cmdb;

gs.print("targetcmdb" + sourceCMDB); getting response as undefined
}


}
}

 

Thanks.

1 ACCEPTED SOLUTION

Sohithanjan G
Kilo Sage
Kilo Sage

hi @AnandKumar1 ,

It seems that the issue is related to the structure of the sourceArr and targetArr arrays. The values you are pushing into these arrays are arrays themselves, and when you try to access sourceArr[k].source_cmdb and targetArr[j].target_cmdb, they result in undefined because there is no source_cmdb or target_cmdb property at the top level of the inner arrays.

 

To resolve this, you should either flatten the arrays or modify how you access the values within the inner arrays. Here's an example of how you might modify your code:

 

var gr = new GlideRecord("sc_req_item");
gr.addQuery('number', "RITM********");
gr.query();

if (gr.next()) {
  var sourceArr = [];
  var targetArr = [];

  var mvrs = gr.variables.u_request_details;

  for (var i = 0; i < mvrs.getRowCount(); i++) {
    var sourceValue = mvrs.getRow(i).getCell('source_cmdb').getCellDisplayValue();
    var valueExtract = sourceValue.split(',');
    sourceArr = sourceArr.concat(valueExtract); // Concatenate arrays directly

    var targetValue = mvrs.getRow(i).getCell('target_cmdb').getCellDisplayValue();
    var trg = targetValue.split(',');
    targetArr = targetArr.concat(trg); // Concatenate arrays directly
  }

  for (var k = 0; k < sourceArr.length; k++) {
    var sourceCMDB = sourceArr[k].trim(); // Trim to remove leading/trailing whitespaces
    gs.print("sourcecmdb " + sourceCMDB);
    // You can use sourceCMDB to get CI names and use them in your API.
  }

  for (var j = 0; j < targetArr.length; j++) {
    var targetCMDB = targetArr[j].trim(); // Trim to remove leading/trailing whitespaces
    gs.print("targetcmdb " + targetCMDB);
  }
}

 

 

Please mark as accepted solution & helpful if its find helpful

BR, Sohith

Please mark as Accepted Solution if this solves your query and HIT Helpful if you find my answer helped you. This will help other community mates too..:)

View solution in original post

5 REPLIES 5

Sohithanjan G
Kilo Sage
Kilo Sage

hi @AnandKumar1 ,

It seems that the issue is related to the structure of the sourceArr and targetArr arrays. The values you are pushing into these arrays are arrays themselves, and when you try to access sourceArr[k].source_cmdb and targetArr[j].target_cmdb, they result in undefined because there is no source_cmdb or target_cmdb property at the top level of the inner arrays.

 

To resolve this, you should either flatten the arrays or modify how you access the values within the inner arrays. Here's an example of how you might modify your code:

 

var gr = new GlideRecord("sc_req_item");
gr.addQuery('number', "RITM********");
gr.query();

if (gr.next()) {
  var sourceArr = [];
  var targetArr = [];

  var mvrs = gr.variables.u_request_details;

  for (var i = 0; i < mvrs.getRowCount(); i++) {
    var sourceValue = mvrs.getRow(i).getCell('source_cmdb').getCellDisplayValue();
    var valueExtract = sourceValue.split(',');
    sourceArr = sourceArr.concat(valueExtract); // Concatenate arrays directly

    var targetValue = mvrs.getRow(i).getCell('target_cmdb').getCellDisplayValue();
    var trg = targetValue.split(',');
    targetArr = targetArr.concat(trg); // Concatenate arrays directly
  }

  for (var k = 0; k < sourceArr.length; k++) {
    var sourceCMDB = sourceArr[k].trim(); // Trim to remove leading/trailing whitespaces
    gs.print("sourcecmdb " + sourceCMDB);
    // You can use sourceCMDB to get CI names and use them in your API.
  }

  for (var j = 0; j < targetArr.length; j++) {
    var targetCMDB = targetArr[j].trim(); // Trim to remove leading/trailing whitespaces
    gs.print("targetcmdb " + targetCMDB);
  }
}

 

 

Please mark as accepted solution & helpful if its find helpful

BR, Sohith

Please mark as Accepted Solution if this solves your query and HIT Helpful if you find my answer helped you. This will help other community mates too..:)