Subtract 1 day from populated due date

booher04
Tera Guru

Afternoon community.  I have a need to populate the due date on SCTASK based off of a date field variable(r_3716_distribute_notice) and then subtract 1 day from that date.  I am trying to use a business rule for this but it's not populating correctly.  I am sure I have something wrong in the code.  I've tried many different codes on my own.

var gdt = current.r_3716_distribute_notice;

if (current.r_3716_distribute_notice){
gdt.gs.daysAgo(1);
task.due_date=gdt;
}

Also tried this:

var gdt = current.r_3716_distribute_notice.getGlideObject();

if (current.r_3716_distribute_notice) {

gdt.subtractDays(1);

current.due_date=gdt;

1 ACCEPTED SOLUTION

dvp
Mega Sage
Mega Sage

Variables is on RITM.. you need to dot walk to ritm first

Here is the updated script

if(current.request_item.variables.r_3716_distribute_notice != undefined){
	var gdt = new GlideDateTime(current.request_item.variables.r_3716_distribute_notice);
	gdt.addDaysLocalTime(-1);
	current.due_date = gdt;
}

View solution in original post

16 REPLIES 16

This is being set to the business rule of the sc_task.  It's set to run before insert/update.  

 

when I updated the "D" that was capitalized, now it blanks out the due_date field and shows nothing.  Ugh.  I can't believe this is giving me this much trouble.

So what info did the logging give you? Is for example already this one wrong?

gs.info('==> date: ' + current.r_3716_distribute_notice);

At what point does the script fail?

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark

---

LinkedIn
Community article list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

I did test now on my PDI, for example below code does give a correct date minus 1 day:

var gr = new GlideRecord('incident');
gr.get('5649ef93db3c0850ceed9ce8db9619dd');

var gdt = new GlideDateTime(gr.sys_created_on);
gs.info('==> gdt: ' + gdt);
gdt.addDaysLocalTime(-1);
gs.info('==> gdt-1: ' + gdt);
gs.info(gdt);

Is your r_3716_distribute_notice field Date or DateTime?

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark

---

LinkedIn
Community article list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

so found that it's not showing on the debug for business rules unless I add it to run before "query" as well.  Once I do that, I can see it and for some reason it's skipping it.  I am going to look at the conditions to see why.

business rule12:04:28 PM.568: Global ==> 'IT Comm Due Date -1 day' on sc_task:
log12:04:28 PM.568: : ==> date: undefined
log12:04:28 PM.570: : ==> gdt:
log12:04:28 PM.570: : ==> gdt-1:
business rule12:04:28 PM.571: Global <== 'IT Comm Due Date -1 day' on sc_task:
business rule12:04:28 PM.571: Finished executing before query business rules on sc_task:
 
That's the results when I took off the conditions.