Tuesday 17 June 2014

How to add validation through trigger in salesforce

Hi All,
This post is regarding to add validation and to display error message through trigger. 
Suppose we want to give validation is no account should be created having name as 'asish'
We can do this by validation rule easily,in some case we want through trigger. Keep in mind trigger should be ours last choice.We should use standard functionality as much as possible.

Here is trigger for above validation:-

trigger ValidateAccount on Account (before insert,before update) {
    for(Account account:Trigger.new){
        if(account.Name == 'asish'){
            account.Name.addError('You can not create account having name as Asish');
        }
    }
}


Here is the output showing error message while creating account.

save image

No comments: