How to trigger task before 1 Business day

Mohammed20
Tera Contributor
Hi,
 
I added the script below in timer activity which is working fine and creating task one day before business day in our dev instance but the same script is not working in test instance, is in the script anything missing?
 
please help on this. Thank you!
answer = 0;
var gdttime = new GlideDateTime();
gdttime.setDisplayValue(current.variables.new_start_date.getDisplayValue());
gs.log("time check: "+gdttime );
gdttime.addaddDays(-1);
var schedulebd = new GlideSchedule('af4477491bad5e10883f11f2b24bcbab');
while (!schedulebd.isInSchedule(gdttime)) {
gs.log("schedule check: "+schedulebd);
gdttime.addDays(-1);
}
//answer = gdttime.getNumericValue();
answer = gs.dateDiff(new GlideDateTime(), gdttime, true);
gs.log("task created one day before business days: "+answer);
8 REPLIES 8

Najmuddin Mohd
Mega Sage

Hi @Mohammed20 ,

Can you check the timezone ? Are both the instances having the same timezone.

 Next, can you try with getValue and setValue, instead of getDisplayValue() and setDisplayValue(). I believe, this issue was sorted by this.

If the above information helps you, kindly mark it as Helpful and Accept the solution.
Regards,
Najmuddin.

Mohammed20
Tera Contributor

Hi Najmuddin,

 

Both instance time are same.

 

I tried with getValue and setValue instead of getDisplayValue() and setDisplayValue() which is not working and also used through directly with var gdttime = new GlideDateTime(current.variables.new_start_date); 

 

I am not sure what exactly the issue in script.

OlaN
Giga Sage
Giga Sage

Hi,

The only error I can spot in the code is this row:

gdttime.addaddDays(-1);

 

Other than that, I would also recommend that you use the getValue and setValue instead of getDisplayValue and setDisplayValue (those are mainly used when presenting the data to an end user, and takes account for differences in timezones).

 

So I tried the code, and made some minor changes (using another schedule (8-5 weekdays), and setting the date manually, because I don't have the variables you use).
Have you verified that your input-date-variable contains data?

var answer = 0;
var gdttime = new GlideDateTime();
gdttime.setValue("2024-11-03 12:00:00");
gs.info("time check: "+gdttime );
gdttime.addDays(-1);
var schedulebd = new GlideSchedule('08fcd0830a0a0b2600079f56b1adb9ae');
while (!schedulebd.isInSchedule(gdttime)) {
  gs.info("schedule check: "+schedulebd);
  gdttime.addDays(-1);
}
//answer = gdttime.getNumericValue();
answer = gs.dateDiff(new GlideDateTime(), gdttime, true);
gs.info("task created one day before business days: "+answer);
gs.info('selected day: ' + gdttime.getValue());

/*  Output of the script below:
*** Script: time check: 2024-11-03 12:00:00
*** Script: schedule check: com.glide.schedules.Schedule@756fd04
*** Script: task created one day before business days: 95504
*** Script: selected day: 2024-11-01 11:00:00
*/

 

Mohammed20
Tera Contributor

Hi  Olan,

 

Thanks for your reply!

I had correct the error, yes the variable getting the proper time as checked in logs through both the way getValue and setValue instead of getDisplayValue() and setDisplayValue().

 

If the day is business day then script is only generating a task one day before and if it is not an business days then generating two days before and does not calculating the properly, for example i have excluded 1st November in cmn_schedule and chose the date in variable for 4th November Monday but the script is not generating a task today which will be create tomorrow 1st November as you can see below.

 

var answer = 0;
var gdttime = new GlideDateTime();
gdttime.setValue("2024-11-04 12:30:00");
gs.info("time check: "+gdttime );
gdttime.addDays(-1);
gs.info("time check: "+gdttime);
var schedulebd = new GlideSchedule('af4477491bad5e10883f11f2b24bcbab');
while (!schedulebd.isInSchedule(gdttime)) {
gs.info("schedule check: "+schedulebd);
gdttime.addDays(-1);
gs.info("time check1: "+gdttime);
}
answer = gs.dateDiff(new GlideDateTime(), gdttime, true);
gs.info("task created one day before business days: "+answer);
gs.info('task created day: ' +gdttime);

 

 

*** Script: time check: 2024-11-04 12:30:00
*** Script: time check: 2024-11-03 12:30:00
*** Script: schedule check: com.glide.schedules.Schedule@51a89dc3
*** Script: time check1: 2024-11-02 11:30:00
*** Script: schedule check: com.glide.schedules.Schedule@51a89dc3
*** Script: time check1: 2024-11-01 11:30:00
*** Script: task created one day before business days: 63039
*** Script: selected day: 2024-11-01 11:30:00