Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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

Hello Jake,

Thanks for the quick reply and fix.

Since I am trying to retrieve the date field,I have used .getValue() and below is my modified script with logs.

var indev='';
var proposition='';
var live='';
  var arr=[];
 
var CIRecord = new GlideRecord('x_inpgh_upmx_pf_lifecycle_breakdown');
CIRecord.addQuery('cmdb_ci','4ac3006487673050d19aecec3fbb356d');
CIRecord.query();
while(CIRecord.next()){
 var obj = {
 
"name":CIRecord.lifecycle.name,
"date":CIRecord.start_date.getValue()
 
};
arr.push(obj);
//arr.push(CIRecord.lifecycle.name+','+CIRecord.start_date);
//arr.push(JSON.stringify(arr));
}
for (i=0;i<arr.length;i++){
    gs.print("Inside FOR>>>>"+arr[i].name + "," +arr[i].date);
    //gs.print(arr[i].name.indexOf('Phase-out'));
    if(arr[i].name.indexOf('In Development')>-1){
indev=arr[i].date;
    }
    if(arr[i].name.indexOf('Proposition')>-1){
proposition=arr[i].date;
    }
    if(arr[i].name.indexOf('Live')>-1){
live=arr[i].date;
    }
}
gs.print("INDEV>>>>"+indev);
gs.print("PROPOSITION>>>>"+proposition);
gs.print("LIVE>>>>"+live);
 
Result in background Script:

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