+ Start a Discussion
hari azmeera 8hari azmeera 8 

custom validation to check field is not empty while saving, if field is empty display error message

Are you using Visualforce page or standard salesforce page Layout
Ravi Dutt SharmaRavi Dutt Sharma
Hey Hari,

There are many ways to achieve this. Listing few of them :

1) You can create a validation rule like given below :

2) Mark the field as required while creating the field.

3) Mark the field as required on page layout,

4) Keep a check in before insert / before update trigger.
Hi hari,
You can use salesforce standard functionality for above requirement.
use validation rule. following is a example.

Error condition formula - ISBLANK(fieldApiName)

User-added image
JyothsnaJyothsna (Salesforce Developers) 
Hi Hari,

Please check the below sample code.

Visualforce page
<apex:page controller="FieldnotEmptycontrl">
<apex:form >
 <apex:pageBlock >
 <apex:pageMessages />
<apex:pageBlockButtons location="bottom">
 <apex:commandButton value="save" action="{!save}"/>
  <apex:pageBlockSection >
  <apex:inputField value="{!s.First_name__c}" required="true"/>

public with sharing class FieldnotEmptycontrl {

    public customer__c s { get; set; }
    public FieldnotEmptycontrl (){
       s=new customer__c();

    public pagereference save() {
    if(s.First_name__c !=null)
     insert s;
     pagereference  pr=new pagereference ('https://ap1.salesforce.com/'+s.id);
       return pr;
          ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Please enter Account number'));
          return null;



User-added image

Hope this helps you!
Best Regards,