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
hari azmeera 8hari azmeera 8 

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

JethaJetha
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 :
 
ISBLANK(FieldAPIName)

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.
YogeshMoreYogeshMore
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:pageBlockButtons>
  <apex:pageBlockSection >
  <apex:inputField value="{!s.First_name__c}" required="true"/>
  </apex:pageBlockSection>
  </apex:pageBlock>
  </apex:form>
</apex:page>

Controller
 
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;
     }
   else
   
    {
          ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Please enter Account number'));
          return null;
     }
       
  
    }

}

Output:

User-added image

Hope this helps you!
Best Regards,
Jyothsna