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.

PA script returning NaN for ageing ticket for ritm excluding weekends and holidays

Harsha Pandey
Tera Expert

Hello,

I am getting 'Nan'  error for a script (pa_scripts). I have written this script to exclude weekends and holidays and added as a breakdown through breakdown mapping in indicator.

Please let me know why I am getting below error and whether I am going correct as per the requirement mentioned above?

var businessDuration = function(open,closed){
   
var startDate = new GlideDateTime(open);
var endDate =  new GlideDateTime(closed);
var schedule = new GlideDateTime();
   
schedule.load('090eecae0a0a0b260077e1dfa71da828');//sys_id of cmn_schedule
   
var duration = schedule.duration(startDate, endDate);//

duration = (duration.getGlideObject().getNumericValue()/24*60*60*1000); //getGlideObject().getNumericValue());
return duration;
};
businessDuration(current.opened_at,current.closed_at);
 
Error :
Error during JavaScript evaluation com.snc.pa.dc.ScriptException: JavaScript evaluation returned: NaN in script
 
Thanks in advance,
3 REPLIES 3

Ankur Bawiskar
Tera Patron
Tera Patron

@Harsha Pandey 

try this-> are you sure the record has both open and closed date?

var businessDuration = function(open, closed) {
    var startDate = new GlideDateTime(open);
    var endDate = new GlideDateTime(closed);
    var schedule = new GlideSchedule();
    
    schedule.load('090eecae0a0a0b260077e1dfa71da828'); // sys_id of cmn_schedule
    
    var duration = schedule.duration(startDate, endDate);
    
    // Convert duration to days
    var durationInDays = duration.getNumericValue() / (24 * 60 * 60 * 1000);
    
    return durationInDays;
};

var duration = businessDuration(current.opened_at, current.closed_at);
gs.info('Business duration: ' + duration);

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi Ankur,

I corrected it with your suggested script but this time I got undefined.

Error during JavaScript evaluation com.snc.pa.dc.ScriptException: JavaScript evaluation returned: Undefined in script.

 

Yes I have taken facts table as sc_req_item which has closed field as well and opened .Below is the screenshot of the PA script . 

Do I need to use closed_at or score_end, any idea about this? Earlier I was using score end but then it gave me error for using current has no arguments passed.

HarshaPandey_0-1743158483356.png

Thanks,

Harsha

Below is the new script for which I got undefined error as mentioned .

 

HarshaPandey_1-1743158736886.png