I want to do the validation rule functionality in apex. Can anyone of you please provide a sample snippet , if this way is possible ..

I need to do the validation for Lead Object fields

Thanks in advance !!

  • What is your experience level with apex? – drakored Jan 8 '16 at 8:54
  • 3 months drakored !! Any level of code would be okay for me !! – user27537 Jan 8 '16 at 8:54
  • I answered and focused around triggers which it sounds like what you need/want, but Tushar's answer focused on a different way of popping errors to a user when in the context of VisualForce. Are you looking for database level validation like validation rules supply or app level validation that only works in your VF app? – drakored Jan 8 '16 at 9:28

This is a pretty wide question since many different types of validation are possible. This answer focuses on how to achieve similar functionality with triggers. Triggers are capable of doing higher level app logic than validation rules, and function quite similarly. They can do validation as well as callouts, DML, etc. They're quite powerful, but they have some specific best practices that are really important to know because of the risk of unhandled exceptions in triggers.

I'll throw out some examples, and if you can think of a specific use case "e.g. I need to filter a name for special characters, or I need to filter out leads from a country we don't sell to" I can help give a better more specific example.

The force.com Cookbook site is an excellent resource for exactly this kind of question. Here is an example "recipe" from the cookbook that filters cc info with regular expressions.


This recipe (cookbook again) shows how to prevent deletion when a candidate is related to an application (example custom objects). It shows a good use of the trigger context variable oldMap.


Of course I have to mention Trailhead as well which is a great tool to learn almost anything you want about Salesforce. Here is the trigger lesson that is most useful to you at this point.


Here is a good example of a trigger checking for duplicate contacts (same would apply to lead with some minor tweaks). It even has bulkification which is very important, especially on a critical object like lead.

before insert Trigger to verify if email already exists, if exists addError (CleanUP)

David Liu, one of the Salesforce MVPs wrote a fantastic blog series covering triggers from end to end.


I would probably be flogged by some friends if I didn't mention that you should use a trigger framework. This is a good practice to learn early and use always. Once triggers are in place, it's a pain to go back and implement structure. I recommend minimalism and structure on core objects in Salesforce, lest you enjoy battling 101 SOQL limit exceptions (nobody should enjoy that kind of debugging nightmare). I like this one from Kevin O'Hara.


Make sure to read the documentation section on triggers too. It has some examples and best practices, and some limitations and helpers (trigger context variables for example) that are good to know.


Bulkification is talked about on this post, it's really important to follow that best practice. The post also contains a lot of other great information about best practices:

General trigger bulkification - best practices


if you are already created the validation rule. Then Just put your code in try catch and use apex:pagemessage in VF page.

Other wise you can check condition in apex and then display the error message. For example:

if(Account.phone == null)
  ApexPages.addmessages(ApexPages.Message(ApexPages.Severity.Error, 'Please enter Phone Number.'));

The answer by @drakored is excellent, as it provides a lot of extensive and very interesting information.

Just add my 2 cents, and summarizing, I would do this by:

  • Creating a 'before insert' and 'before update' trigger of leads
  • Inside the trigger, do the validation by using some Apex logic. If the validation fails, then use the sObject.AddError() method on that record. That indicates Salesforce that the insert/update should fail.
  • Show the error in your VF page with the <apex:errorMessage> tag

Not the answer you're looking for? Browse other questions tagged or ask your own question.