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

Sent from my iPhone


Bob, there must've been a problem with your reply


Are you filtering the logs on source='RFS Query'? I forgot to add the source parameter to the log statement I put in the if so you wouldn't see it if you have applied a source filter to your log query



Sent from my iPhone


Yes, the last log picture demonstrates how I filtered the logs typing "rfs"....


Even though the log isn't working, I know the state is being changed, which is what I wanted. You've been extremely helpful Bob. Thanks for helping solve this issue.