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

Thanks Bob. It was working after doing what you suggested but somehow the log is now returning undefined again for the "created" date, which I don't understand why. Also, I'm changing the value of the "state" to "2" and the log still return the original value of "1".



find_real_file.png


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 = task.sys_created_on;


created.addDaysUTC(15);



if (task.next()) {



if(dayLimit>created){


task.state = 2;



}



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




}







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


 


 


}  


Excellent Bob! The dates seem to have worked nicely, however the log "gs.log('In day limit check, setting state to ' + task.getValue('state'));" didn't go through. I reversed the condition so that it goes through as true, but it didn't work. Also, why are my logs always returned twice instead of only once.



Thanks!


Ok, are you sure there is a task that meets your query criteria? Maybe we are not even getting into the task.next conditional.   I would suggest logging out the row count from your task query right after the query() statement:



  1. var task = new GlideRecord('sc_task');  
  2. task.addQuery('request_item', current.sys_id);  
  3. task.addQuery('state', 1);  
  4. task.query();
  5. gs.log(task.getRowCount()); ==> confirm this is greater than 0


Not sure about the double logging; can you post the current version of your script and the log output after running?


Sure thing. Below is the updated code with a picture of its logs. I replicated the query by filtering the table and it DOES display the record I was expecting. Of course, the record demonstrates in its history how the state changed from "open" (1) to "work in progress" (2) which means that the code went through the "if" condition but somehow didn't return a log



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


gs.log(task.getRowCount()); //==> confirm this is greater than 0




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




}    




find_real_file.png