Interested in a ServiceNow event built for developers? Registration for now[dev]26 is officially open!

Dates Validation using Client scripts and Data Policy

Lucky1
Tera Guru

Hello all,

 

On a change request, I want to make sure the difference between Start date and End date to be at least 15 mins but not below that.

It should be applicable during a change request record creation using Integration also.

So, I guess Data policy will work.

So, can someone please give me the code for this scenario.

 

 

Regards,

Lucky

1 ACCEPTED SOLUTION

Nishant8
Tera Sage

Hello @Lucky1 , You can write a Business Rule - configured to run before Insert and Update. you can use below script

 

(function executeRule(current, previous /*null when async*/) {
var startDate = current.work_start;
var endDate = current.work_end;
var startGDT = startDate.getGlideObject();
var endGDT = endDate.getGlideObject();
var compareTime = 900000; // 15 mins milliseconds
var duration = GlideDateTime.subtract(startGDT, endGDT).getNumericValue();
if (duration < compareTime) {
gs.addErrorMessage("Date difference should be more than 15 mins");
}
})(current, previous);

 

Regards,

Nishant

View solution in original post

14 REPLIES 14

Nishant8
Tera Sage

Hello @Lucky1 , You can write a Business Rule - configured to run before Insert and Update. you can use below script

 

(function executeRule(current, previous /*null when async*/) {
var startDate = current.work_start;
var endDate = current.work_end;
var startGDT = startDate.getGlideObject();
var endGDT = endDate.getGlideObject();
var compareTime = 900000; // 15 mins milliseconds
var duration = GlideDateTime.subtract(startGDT, endGDT).getNumericValue();
if (duration < compareTime) {
gs.addErrorMessage("Date difference should be more than 15 mins");
}
})(current, previous);

 

Regards,

Nishant

Hi Nishant,

 

But even if we write BR, this logic won't get executed if the record is creating from external source.

Am I right?

 

Please correct me

 

 

Regards,

Lucky

@Lucky1 

business rule will be evaluated even if record is getting created/updated unless somebody adds this condition in BR

gs.getSession().isInteractive()

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

Hello @Lucky1 , As Ankur confirmed already it'd be executed even for external requests, this approach should satisfy your ask.

 

Regards,

Nishant

Hi Nishant,

 

Ok, So, I will go with the BR,

But should I write this line also as Ankur replied

gs.getSession().isInteractive()

 

 

Regards,

Lucky