Set due date from record producer field date (X days before date)?

Community Alums
Not applicable

Hi Team,

I have a date field on a record producer, and what I would like to achieve is to set the due date on the created ticket 5 days before the date field on the record producer.

I attempted this through a business rule script -

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

	var sd = new GlideDateTime();
	sd.setValue('rec_prod_field_name');
	sd.addDaysUTC(-5);
	current.due_date=sd.getDisplayValue();

})(current, previous);

Can someone please advise the best way to do this and how?!

Thanks,
Alex

1 ACCEPTED SOLUTION

Gunjan Kiratkar
Kilo Patron
Kilo Patron

Hi @Alex ,

You can try the below Code :  

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

var sd = new GlideDateTime(current.variables.date_variable_name);  //Write down Variable name instead of 'date_variable_name' from record producer.

sd.addDaysUTC(-5);

current.due_date=sd.getDisplayValue();

})(current, previous);

 

Please mark my answer as Correct/Helpful based on impact.

 

Regards,

Gunjan Kiratkar

   


Please Mark My Response as Correct/Helpful based on Impact
Regards,
Gunjan Kiratkar
2X ServiceNow MVP
Community Rising Star 2022
Youtube : ServiceNow Guy

View solution in original post

5 REPLIES 5

Abhijit4
Mega Sage

You can't access record producer variables directly in BR, you will have to glide into question_answer table to get value. so instead you can use below script in record producer script section, that will be easy solution for you

var sd = new GlideDateTime(producer.rec_prod_field_name);

sd.addDaysUTC(-5);

current.due_date=sd.getDisplayValue(); OR current.due_date=sd;

Let me know if you have any further queries.

Please mark this as Correct or Helpful if it helps.

Thanks and Regards,
Abhijit
Community Rising Star 2022

By marking my response as correct or helpful, you contribute to helping future readers with similar issues.
Regards,
Abhijit
ServiceNow MVP

Instead of writing new BR, you can do above in record producer script.

By marking my response as correct or helpful, you contribute to helping future readers with similar issues.
Regards,
Abhijit
ServiceNow MVP

Kristy Merriam
Administrator
Administrator

Or for referencing variables on a record producer, you would make a minor change to the above:

var sd = new GlideDateTime(producer.rec_prod_field_name);
sd.addDaysUTC(-5);
current.due_date = sd.getDisplayValue();

Gunjan Kiratkar
Kilo Patron
Kilo Patron

Hi @Alex ,

You can try the below Code :  

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

var sd = new GlideDateTime(current.variables.date_variable_name);  //Write down Variable name instead of 'date_variable_name' from record producer.

sd.addDaysUTC(-5);

current.due_date=sd.getDisplayValue();

})(current, previous);

 

Please mark my answer as Correct/Helpful based on impact.

 

Regards,

Gunjan Kiratkar

   


Please Mark My Response as Correct/Helpful based on Impact
Regards,
Gunjan Kiratkar
2X ServiceNow MVP
Community Rising Star 2022
Youtube : ServiceNow Guy