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

palmen
Tera Guru

Impressive that 3 people give the exact same advice on the same minute 🙂

Lol yea... and all leaders for the current month, go figure, haha.

I'm glad to see people pointing people to use the conditional tools instead of always scripting. Not that I'm against it, but if you can use the tool instead...just do that!


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

Yeah it's best to use the condition builders when possible, makes it easier for everyone to understand. That's what ServiceNow want at least, you shouldn't have to be a developer to be able to do easier tasks in ServiceNow.

Debbi L
Tera Expert

Thanks, all!

Here's the problem. I *did* use only the conditional tools initially, but it exhibited the same behavior. (And, for clarification, once I went to scripting, I used both (!current.legal_start_date.nil()) AND (current.legal_start_date.nil()) based upon information given by others.

I've just tried it again, using conditional tools. I set the script to the following, and experienced the same results:

 

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


current.legal_start_date = new GlideDateTime();


})(current, previous);

 

Change the order of the BR, may be some other BR is clearing out the value set by this one.

-Anurag