Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Unable to use addDays() addDaysUTC() to modify due date using a business rule

Zijadx
Kilo Explorer

Hello, 

I ran into an issue while trying to set the due date on an incident form using a business rule. 

The workflow:

  • automatically create a new incident whenever an incident with the short description "test" is created (done)
  • pre-fill fields on the new incident using a business rule (done)
  • set the due date on the new incident to be 2 weeks before the due date on the original incident (not done) 

The third item is where I ran into an issue, I am unable to manipulate the due date from the original ticket, the new incident either has the same due date as the original or a blank due date. 

 

Code:

(function executeRule(current, previous /*null when async*/) {

//Generate the new incident
var gr = new GlideRecord('incident');

gr.initialize();

//set fields on the form 
gr.short_description = 'Created via business rule - Short Description';
gr.description = 'Created via business rule - Description';
gr.impact = '2 - Medium';
gr.urgency = '1 - High';
gr.assignment_group = 'f33a0002db2077884af1fd33399619a8';

//set due date to be 14 days less than the due date on the original incident 

var gdt = current.due_date;
newGdt = gdt.addDays(-14);
gr.due_date = newGdt;

gr.insert(); //insert the new record

})(current, previous);

 

//I also tried the following for the due date portion: 

gr.due_date = current.due_date.addDays(-14);

gdt2 = gdt.addDays(-14);
gr.due_date = gdt2;

//tried this

var gdt = current.due_date.getGlideObject();

gdt2 = gdt.addDays(-14);
gr.due_date = gdt2;

 

What am I missing? also note that I am able to use a new glide date time and modify it with no issues, for example: I can take the current date and time, add or subtract days from it and add have it on the form with no issues. I am only running into the issue above if I try to modify a manually set date. 

 

Thanks,

3 REPLIES 3

Mike Patel
Tera Sage

Did you tried

var gdt = new GlideDateTime(current.due_date);
var newGdt = gdt.addDays(-14);

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

addDays() and addDaysUTC() doesn't return anything; can you update as below and try once

var gdt = new GlideDateTime(current.due_date);

gdt.addDays(-14);

gr.due_date = gdt;

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Ian Mildon
Tera Guru

I second Mike's suggestion of using the following to define your current.due_date. Anytime you are wanting to run script against a date value, you need to define it as a GlideDateTime value.

var gdt = new GlideDateTime(current.due_date);

Additionally, when you are setting the new date value you need to add ".getValue();" so in your example I would make the last line:

gr.due_date = gdt2.getValue();