Add days to a date

samuelscott
Tera Expert

Hello,

 

I'm working on a script action where I'm trying to add days to the date of creation. Somehow, the syntax I'm using of "addDays();" is not working and when debugging, the logs display "undefined". I'll attach a picture of the debug logs. Could someone please help add days to the date? Thanks!  

 

iDate(); function iDate(){   var created = new GlideDate(); var dayLimit = new GlideDate();//va a ser igual que el "process on" y "process on" es siempre el dia en el que se abre el task + 15     var task = new GlideRecord('sc_task');   task.addQuery('request_item', current.sys_id);   task.addQuery('state', 1);//(OPEN) es el ultimo task   task.query();     if (task.next()) { //cuando encuentres el primer match, entra a ese registro       created = task.sys_created_on; //task.sys_created_on; //task ya tiene el objeto del registro   if(dayLimit>created.addDaysUTC(15)){//created.addDaysUTC() no esta jalando   task.state = 2;       }     task.update();       }     var str = 'RFS created date: ' + created.toString();   str += ', Sys created (sin string): ' + created;   str += ', Day Limit: ' + dayLimit;       gs.log (str,'RFS query'); }
1 ACCEPTED SOLUTION

Hmm...that should work, I ran a similar script in test instance of mine and it logs out fine.   At any rate, I think the larger issue is with the state setting, this is failing due to the way you are setting the created date from task.sys_created_on.   You can't do this with a direct assignment (created = task.sys_created_on), you need use the setValue method of GlideDate.   This is causing the date to not be set correctly and created stays set to the current date time when the object is created.



Try the following, I broke up the log statements so we can see what the dates are right before the dayLimit > created check and the state after it is set in the conditional.



iDate();  


function iDate(){  


 


 


var dayLimit = new GlideDate();  


var created = new GlideDate();  


 


 


 


 


var task = new GlideRecord('sc_task');  


task.addQuery('request_item', current.sys_id);  


task.addQuery('state', 1);  


task.query();    


 


created.setValue(task.sys_created_on);    


created.addDaysUTC(15);




gs.log('RFS created ' + created.getValue(), 'RFS query');


gs.log('Day Limit ' + dayLimit.getValue(), 'RFS query');  


 


if (task.next()) {    


 


if(dayLimit>created){  


task.state = 2;  


  gs.log('In day limit check, setting state to ' + task.getValue('state'));


}  




task.update();  


 


 


}  


 


var str = 'RFS created date: ' + created.getValue() + '\n';  


str += 'Day Limit: ' + dayLimit + '\n';  


str += 'Task State: ' + task.state;    


 


gs.log (str,'RFS query');  


 


 


}  


View solution in original post

16 REPLIES 16

No problem Samuel; glad to be of help.   Sorry, I was on my phone earlier and couldn't see the picture of the logs.   Since you did have the rfs query filter on the logs, you will need to add the 'RFS Query' source parameter to the log statement I put in there so it is captured by the query...like so:



gs.log('In day limit check, setting state to ' + task.getValue('state'), 'RFS Query');




Aditya Telideva
ServiceNow Employee
ServiceNow Employee

Also the following post may help u:


How can I add days to a date field?