function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sam AlexSam Alex 

Validate Contact object email if a custom field is checked

Hi All,

I have added a check box called "ValidateEmail" to Contact Object. When "ValidateEmail" is TRUE I want the before update and before insert trigger to check whether the EMAIL (field on Contact object) is empty or not.

If the "ValidateEmail" is TRUE and EMAIL is empty I dont want to add/update that record. Instead I want to show a validation error message. If the "ValidateEmail" is FALSE I dont want that validation.

How can I do this?

Any Help? Thanks in advance.

Anoop yadavAnoop yadav
Hi,
Try with the below Trigger
trigger validateEmail on Contact(before insert, before update){

	for(Contact  con: trigger.new){ 
		if(con.ValidateEmail__c && con.Email== ''){
			con.addError('Your Error message');
		}
    }
}


 
StephenKennyStephenKenny
Hi Sam, 
It sounds like you can achieve this with a simple validation rule on the Contact object so there is no need to write a trigger for this. A validation rule is best practice in this scenario as it is a 'Clicks not Code' approach which is easier for a system administrator to maintain. 

Validation Rule
ValidateEmail__c == True && isBlank(Email)