Need help with the script

Nani5
Tera Expert

I have a catalog Item where in I have 3 different variables (Indevelopment,Preposition,Live).I have a table "which stores the dates in multiple records meaning Indevelopment of CI will have 1 record with start date,Preposition of CI will have 1 record with start date etc...

Need to retrieve the start dates and fill it in the Item form.Tried with below script but multiple record values I need in same form.

var indev='';
var preposition='';
var live='';
var CIRecord = new GlideRecord('x_inpgh_upmx_pf_lifecycle_breakdown');
CIRecord.addQuery('cmdb_ci','4ac3006487673050d19aecec3fbb356d');
CIRecord.query();
while(CIRecord.next()){
    var arr=[];
arr.push(CIRecord.lifecycle.name+','+CIRecord.start_date);
gs.print("ARRAY>>>>>>"+arr.toString());
for (i=0;i<arr.length;i++){
    gs.print("Inside FOR>>>>"+arr[i]);
}
}
AdityaKumarVa_0-1722927031688.png

 

1 ACCEPTED SOLUTION

@Nani5,

 

For each record create an object and push it into the array like this:

while(CIRecord.next()){
 
var obj = {
 
"name":CIRecord.lifecycle.name,
"date":CIRecord.start_date
 
};
arr.push(obj);
 
}
 
access the values like this:
 
for (i=0;i<arr.length;i++){
    gs.print("Inside FOR>>>>"+arr[i].name + " " arr[i].date);
}

View solution in original post

5 REPLIES 5

Jake Sadler
Kilo Sage

Hello @Nani5 ,

 

You are declaring the "arr" variable inside the while loop and trying to loop through it after pushing an object once.

 

Move the arr variable outside the while loop before the gliderecord.

 

Move the for loop so that it is after the while loop.

 

Then you can push the start dates into the array and return nit to your script for processing.

 

var indev='';
var preposition='';
var live='';
  var arr=[];
 
var CIRecord = new GlideRecord('x_inpgh_upmx_pf_lifecycle_breakdown');
CIRecord.addQuery('cmdb_ci','4ac3006487673050d19aecec3fbb356d');
CIRecord.query();
while(CIRecord.next()){
 
arr.push(CIRecord.lifecycle.name+','+CIRecord.start_date);
 
}
for (i=0;i<arr.length;i++){
    gs.print("Inside FOR>>>>"+arr[i]);
}

Hello Jake,

Thanks for your response.

I have tried in that way aswell.Below is the result I am getting when I run in Background script.How to extract the date fields and set it in the right variables?

Result in Background script:


*** Script: Inside FOR>>>>Proposition,
*** Script: Inside FOR>>>>Live,2023-01-31
*** Script: Inside FOR>>>>In Development,2021-10-28

@Nani5,

 

For each record create an object and push it into the array like this:

while(CIRecord.next()){
 
var obj = {
 
"name":CIRecord.lifecycle.name,
"date":CIRecord.start_date
 
};
arr.push(obj);
 
}
 
access the values like this:
 
for (i=0;i<arr.length;i++){
    gs.print("Inside FOR>>>>"+arr[i].name + " " arr[i].date);
}

@Nani5 if my answer helped please mark it as correct