GlideDateTime methods not working in Fix Script but are in business rule

bookman1502
Giga Expert

I'm trying to populate a custom field called "Refresh Date" that will be shown to end users on the portal with the quarter and year based on the warranty expiration date. The business wants to do this so that users aren't jumping down the throat of the service desk as soon as the warranty is expired; we'd like to just give them a rough estimate instead of a date. We have a business rule that runs on display for queries which populates this Refresh Date field based on the warranty expiration date:

(function executeRule(current, previous /*null when async*/) {


getYearAndMonth();

})(current, previous);

function getYearAndMonth(){
var gdt =current.warranty_expiration.getGlideObject();
var year = gdt.getYear();
var month = gdt.getMonth();
var quarter;

if((month==1) ||(month==2) ||(month==3)){
current.u_refresh_date = 'Q1'+ "-" +year;
current.update();
}
else if((month==4)|| (month==5) ||(month==6)){
current.u_refresh_date = 'Q2'+ "-" +year;
current.update();
}
else if ((month==7)||(month==8) ||(month==9)){
current.u_refresh_date = 'Q3'+ "-" +year;
current.update();
}
else if ((month==10)||(month==11) ||(month==12)){
current.u_refresh_date = 'Q4'+ "-" +year;
current.update();
}
}

This code works in the business rule. However, when I tried to put this code in a fix script (changing "current" to a GlideRecord object of course) it gave me errors for the getYear() and getMonth methods. I modified these to getFullYear() and getFullMonth(), which fixed the Javascript error but the logs just show them being populated with the "undefined" value. Here's the fix script:

var refreshdatecomputers = new GlideRecord('u_cmdb_ci_computersystem');
refreshdatecomputers.query();
while(refreshdatecomputers.next()){
var gdt =refreshdatecomputers.warranty_expiration.getGlideObject();
var year = gdt.getFullYear();
gs.log('year: '+year);
var month = gdt.getFullMonth();
gs.log('month: '+month);
var quarter;
gs.log('quarter: '+quarter);
if((month==1) ||(month==2) ||(month==3)){
refreshdatecomputers.u_refresh_date = 'Q1'+ "-" +year;
gs.log('Q1');
refreshdatecomputers.update();
}
else if((month==4)|| (month==5) ||(month==6)){
refreshdatecomputers.u_refresh_date = 'Q2'+ "-" +year;
refreshdatecomputers.update();
gs.log('Q2');
}
else if ((month==7)||(month==8) ||(month==9)){
refreshdatecomputers.u_refresh_date = 'Q3'+ "-" +year;
refreshdatecomputers.update();
gs.log('Q3');
}
else if ((month==10)||(month==11) ||(month==12)){
refreshdatecomputers.u_refresh_date = 'Q4'+ "-" +year;
refreshdatecomputers.update();
gs.log('Q4');
}
}

Any help would be much appreciated.

 

 

 

 

 

1 ACCEPTED SOLUTION

Ok I got it working by converting the warranty_expiration field to a string, doing a split on '-' and then assigning the year and month the values from the array:

var refreshdatecomputers = new GlideRecord('u_cmdb_ci_computersystem');
refreshdatecomputers.addNotNullQuery('warranty_expiration');
refreshdatecomputers.query();
while(refreshdatecomputers.next()){
var gdt = refreshdatecomputers.warranty_expiration.toString();
//gs.log('gdt: '+gdt);
var datearr = gdt.split('-');
//gs.log('date array: '+datearr);
var year = datearr[0];
//gs.log('year: '+year);
var month = datearr[1];
//gs.log('month: '+month);
var quarter='';
if((month=='01') ||(month=='02') ||(month=='03')){
refreshdatecomputers.u_refresh_date = 'Q1'+ "-" +year;
refreshdatecomputers.update();
gs.log(year+' '+month+' '+quarter);
gs.log('Q1');
}
else if((month=='04')|| (month=='05') ||(month=='06')){
refreshdatecomputers.u_refresh_date = 'Q2'+ "-" +year;
refreshdatecomputers.update();
gs.log(year+' '+month+' '+quarter);
gs.log('Q2');
}
else if ((month=='07')||(month=='08') ||(month=='09')){
refreshdatecomputers.u_refresh_date = 'Q3'+ "-" +year;
refreshdatecomputers.update();
gs.log(year+' '+month+' '+quarter);
gs.log('Q3');
}
else if ((month=='10')||(month=='11') ||(month=='12')){
refreshdatecomputers.u_refresh_date = 'Q4'+ "-" +year;
refreshdatecomputers.update();
gs.log(year+' '+month+' '+quarter);
gs.log('Q4');
}
}

View solution in original post

3 REPLIES 3

Curtis_Myers
Tera Expert

I was able to get results using the getYear() and getMonth() instead of the JavaScript getFullYear() or the getFullMonth().

I believe that you undefined error is caused by the fact that getFullMonth() and getFullYear() are intended to get the Month or Year from a Date object instead of GlideObject

 var refreshdatecomputers = new GlideRecord('u_cmdb_ci_computersystem');
refreshdatecomputers.query();
while(refreshdatecomputers.next()){
 var gdt = refreshdatecomputers.warranty_expiration.getGlideObject();
 var year = gdt.getYear();
 gs.log('year: '+year);
 var month = gdt.getMonth();
 gs.log('month: '+month);
 var quarter='';
 if((month<=3)){
  refreshdatecomputers.u_refresh_date = 'Q1'+ "-" +year;
  quarter='Q1';
  refreshdatecomputers.update();
 }
 else if((month<=6)){
  refreshdatecomputers.u_refresh_date = 'Q2'+ "-" +year;
  refreshdatecomputers.update();
  quarter='Q2';
 }
 else if ((month<=9)){
  refreshdatecomputers.u_refresh_date = 'Q3'+ "-" +year;
  refreshdatecomputers.update();
  quarter='Q3';
 }
 else if ((month<=12)){
  refreshdatecomputers.u_refresh_date = 'Q4'+ "-" +year;
  refreshdatecomputers.update();
  quarter='Q4';
 }
 gs.log('quarter: '+quarter);

Thanks for the interest Curtis. Did you create a custom "u_cmdb_ci_computersystem" table in a personal instance to get that working? I don't remember us creating our own before ServiceNow created cmdb_ci_computersystem. At any rate, when I use getYear() or getMonth() instead of getFullYear() or getFullMonth() it gives me a null pointer exception, which is the error I was talking about the in the post which caused me to try getFullYear() and getFullMonth(). Your comment about those methods requiring a Date object instead of a GlideObject have caused me to look into trying to turn the value of warranty_expiration into a Date object. I've tried leaving it as is, appending getDate, and appending getDateTime() but all those cause month and year to have undefined values. I might elevate it to a HI ticket if there's something about the instance causing this.

Ok I got it working by converting the warranty_expiration field to a string, doing a split on '-' and then assigning the year and month the values from the array:

var refreshdatecomputers = new GlideRecord('u_cmdb_ci_computersystem');
refreshdatecomputers.addNotNullQuery('warranty_expiration');
refreshdatecomputers.query();
while(refreshdatecomputers.next()){
var gdt = refreshdatecomputers.warranty_expiration.toString();
//gs.log('gdt: '+gdt);
var datearr = gdt.split('-');
//gs.log('date array: '+datearr);
var year = datearr[0];
//gs.log('year: '+year);
var month = datearr[1];
//gs.log('month: '+month);
var quarter='';
if((month=='01') ||(month=='02') ||(month=='03')){
refreshdatecomputers.u_refresh_date = 'Q1'+ "-" +year;
refreshdatecomputers.update();
gs.log(year+' '+month+' '+quarter);
gs.log('Q1');
}
else if((month=='04')|| (month=='05') ||(month=='06')){
refreshdatecomputers.u_refresh_date = 'Q2'+ "-" +year;
refreshdatecomputers.update();
gs.log(year+' '+month+' '+quarter);
gs.log('Q2');
}
else if ((month=='07')||(month=='08') ||(month=='09')){
refreshdatecomputers.u_refresh_date = 'Q3'+ "-" +year;
refreshdatecomputers.update();
gs.log(year+' '+month+' '+quarter);
gs.log('Q3');
}
else if ((month=='10')||(month=='11') ||(month=='12')){
refreshdatecomputers.u_refresh_date = 'Q4'+ "-" +year;
refreshdatecomputers.update();
gs.log(year+' '+month+' '+quarter);
gs.log('Q4');
}
}