Setting date/time field using business rule only if the field is blank

Debbi L
Tera Expert

I'm attempting to set a "start date" date/time field to the current date/time based upon the criteria that one field is true, and the "start date" field is empty. I've been able to make it set the date/time initially (that part works perfectly); however, every time I send a new update, it changes the date to the current time, even though the "start date" field is populated. I've done an exhaustive search, and can find nothing that remediates this. Help!

I'm sure I'm missing something simple. This is the relevant code:

__________

When: Before, on Insert or Update

I've done this with and without filter conditions: Send to Legal is true, Legal start date is empty

_________

 

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

if (current.send_to_legal=='true' && (!current.legal_start_date.nil())) {

current.legal_start_date = gs.nowDateTime();
}

})(current, previous);

1 ACCEPTED SOLUTION

palmen
Tera Guru

You can do this with condition on the BR so it only runs if it's empty

find_real_file.png

View solution in original post

20 REPLIES 20

Anurag Tripathi
Mega Patron
Mega Patron

Make sure that the if cindition is satisfied, and try using this line instead

 

current.legal_start_date = new GlideDateTime();

-Anurag

palmen
Tera Guru

You can do this with condition on the BR so it only runs if it's empty

find_real_file.png

Allen Andreas
Administrator
Administrator

Hi,

Should be a before business rule and you can literally use the conditions there, instead of scripting that, for it to fire on "Update".

Plus you're also saying not nil....with that line.

So something like:

Then under actions, you can have that one line: current.legal_start_date = gs.nowDateTime(); 

 Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hello Debil,

 

You can achieve the above requirement without coding. You can set up this as part of filter condition. Sample screenshot below for reference.

find_real_file.png

 

Under the Action tab you can use set field values to set the field on the form.

 

-Pradeep Sharma