- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-06-2023 12:51 AM
I have a table Employee with two fields EmployeeSD and EmployeeED - both are date fields. EmployeeSD is a mandatory field. Now I need to write a business rule which will prevent the submission of record when EmployeeSD is after EmployeeED. I have written the following script:
(function executeRule(current, previous /*null when async*/) {
var empSD = current.u_glide_date_1;
var empED = current.u_glide_date_2;
if (empSD && empED && empSD > empED) {
if (current.operation() == "insert") {
gs.addErrorMessage("Employment SD cannot be after Employment ED");
return false;
}
else {
gs.addInfoMessage("Cannot update record: Employment SD is after Employment ED");
return;
}
}
})(current, previous);
When I update the record, it does give me the warning but also updates the record. How can I prevent that from happening?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-06-2023 01:01 AM - edited 03-06-2023 01:07 AM
update script as this
(function executeRule(current, previous /*null when async*/) {
var empSD = new GlideDateTime(current.u_glide_date_1);
var empED = new GlideDateTime(current.u_glide_date_2);
if (empSD.getNumericValue() > empED.getNumericValue()) {
if (current.operation() == "insert") {
gs.addErrorMessage("Employment SD cannot be after Employment ED");
current.setAbortAction(true);
}
else {
gs.addInfoMessage("Cannot update record: Employment SD is after Employment ED");
current.setAbortAction(true);
}
}
})(current, previous);
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-06-2023 01:01 AM - edited 03-06-2023 01:07 AM
update script as this
(function executeRule(current, previous /*null when async*/) {
var empSD = new GlideDateTime(current.u_glide_date_1);
var empED = new GlideDateTime(current.u_glide_date_2);
if (empSD.getNumericValue() > empED.getNumericValue()) {
if (current.operation() == "insert") {
gs.addErrorMessage("Employment SD cannot be after Employment ED");
current.setAbortAction(true);
}
else {
gs.addInfoMessage("Cannot update record: Employment SD is after Employment ED");
current.setAbortAction(true);
}
}
})(current, previous);
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-06-2023 01:02 AM
Hi @vidhya_mouli,
Please add the current.setAbortAction(true); line inside the condition after throwing the error message.
"return" is not required.
Also please note that business rule must be a before business rule.
Let me know if it was helpful.
Thanks
Nootan