How to display/show an error message at the respective field through trigger salesforce? - Salesforce Globe For You
Problem: Assume while inserting Lead record,in case if Email field of Lead is empty ,need to display an error Message 'Please Enter Email' below the Email field itself.
Solution: use addError() method at it's respective record field to display Error message like this objLead.Email.addError('Please Enter Email'); where Email is field where error message to be displayed.
Example:
trigger ErrorMessageAtFieldLevel on Lead (before insert) {
for(Lead objLead :trigger.new) {
if(String.isBlank(objLead.Email)) {
objLead.Email.addError('Please Enter Email');
}
}
}
Output: when you try creating a new Lead record without entering Email,it will through an error message at the Email field as shown below.
No comments:
Post a Comment