Record-Level Validation Code Examples
The following custom validation logic checks whether records have a null io_application field value, and if so, logs an error and adds a validation error by calling addFieldValidationError. In this example, recordUnderValidation contains the record to be validated, and logger grants access to log4jlogger.
package com.servicemax.example;
import com.servicemax.core.validator.OperationValidator;
import java.util.Map;
class MyValidator extends OperationValidator {
public Object realValidator(Map<String, Object> params) {
//verify whether record's application value is null
if(recordUnderValidation.io_application == null) {
//if the value is null, log the error
logger.error("The Application field is blank for Source object {}", recordUnderValidation.io_name)
//and generate a field validation error to be displayed by the UI
addFieldValidationError('io_application', "The \"Application\" Field can not be null.") // This adds an error to the validation, then record will not be saved
}
}
}
The following custom validation logic blocks field updates based on the values of other fields within the same records, such as changes to the Start and End fields of an Appointment record with a Status field value of Accepted.
package com.servicemax.example;
import com.servicemax.core.validator.OperationValidator;
import java.util.Map;
class MyAppointmentValidator extends OperationValidator {
public Object realValidator(Map<String, Object> params) {
//If the status of the appointment is Accepted
if(recordUnderValidation.svmx_workflow.toString() == "Accepted") {
//verify that the Start and End Time fields are not being changed
if(recordUnderValidation.isFieldChanged("svmx_start_datetime")) {
//if Start is trying to be changed, generate a field validation error to be displayed by the UI
addFieldValidationError('svmx_start_datetime', "The \"Start\" field cannot be changed if Status is Accepted.") // This adds an error to the validation, then record will not be saved
}
if(recordUnderValidation.isFieldChanged("svmx_end_datetime")) {
//if End is trying to be changed, generate a field validation error to be displayed by the UI
addFieldValidationError('svmx_start_datetime', "The \"End\" field cannot be changed if Status is Accepted.") // This adds an error to the validation, then record will not be saved
}
}
}
}
For more information: