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
Nehru komuNehru komu 

Hi,i am writing an apex trigger in which i need to validate that account name and account number is already exist. if not the record should not be inserted and rise an error invalid account number and account name

AvaneeshAvaneesh
Hi Nehru
here is trigger for account name validation 
trigger triggerForValidation on Account (before insert) {
   Set<String> allAccountName = new Set<String>();  
    for(Account acc:[select id,name from Account]){
        if(acc.name !=NULL){
         allAccountName.add(acc.Name);   
        }  
    }    
    for(account acc:Trigger.new){
        if(acc.Name !=NULL && allAccountName.contains(acc.Name)){
            acc.addError('acccount already exists');
        }
    }
}
if this was helpful mark as best answer else let me know 

Thank you
Avaneesh Singh

 
Nehru komuNehru komu
i am inserting record only the account already exists