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

samuelscott
Tera Expert

find_real_file.png


iDate();


function iDate(){




var created = new GlideDate();


var dayLimit = new GlideDate();





var task = new GlideRecord('sc_task');


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


task.addQuery('state', 1);


task.query();




if (task.next()) {



created = task.sys_created_on;


if(dayLimit>created.addDaysUTC(15)){


task.state = 2;



}



task.update();



}




var str = 'RFS created date: ' + created.toString().addDaysUTC(15);


str += ', Sys created (sin string): ' + created.addDaysUTC(15);


str += ', Day Limit: ' + dayLimit;



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


}


Hi Samuel,



I don't think the issue is with the addDays() method.   What is returning undefined is the dayLimit variable and I do not see where you are applying addDays to this GlideDate object (i see it applied to the created GD).   Are you sure you had the dayLimit variable defined when you ran your test?


I'm sorry, let me show you an updated code with its debugging logs. When I tried adding days to the other dates besides "Day Limit" they all returned undefined.



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);


task.query();




if (task.next()) {



created = task.sys_created_on;


if(dayLimit>created.addDaysUTC(15)){


task.state = 2;



}



task.update();



}




var str = 'RFS created date: ' + created.toString().addDaysUTC(15) + '\n';


str += 'Sys created (sin string): ' + created.addDaysUTC(15) + '\n';


str += 'Day Limit: ' + dayLimit.addDaysUTC(15) + '\n';


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



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




}




find_real_file.png


Hi Scott,



Ah ok, thanks.   The issue is that the addDaysUTC method does not have a return value which is what you are trying to include in your log output string.   You would need to use the getValue() method of GlideDate after applying addDays and include this in your log output string like so:



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




created.addDaysUTC(15);


dayLimit.addDaysUTC(15)


 


 


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


str += 'Sys created (sin string): ' + created.getValue() + '\n';  


str += 'Day Limit: ' + dayLimit.getValue() + '\n';      


 


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


 


 


}