Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Prevent updation of record based on condition.

vidhya_mouli
Tera Sage

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?

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@vidhya_mouli 

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.

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

View solution in original post

2 REPLIES 2

Ankur Bawiskar
Tera Patron
Tera Patron

@vidhya_mouli 

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.

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

Nootan Bhat
Kilo Sage

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