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
Rutvij Pathak 5Rutvij Pathak 5 

Apex field validation not working

Hi , I am trying to insert list of Contacts from visualforce page on a Button click. And after button click the page should automatically go back to account page.

I have Created a Custom Button on an Account page.

The list also has a Validation to check if the first name is updated or not.

Basically - 1 - Contact First name must not be blank.
2 - On Click of Save, Added Contacts should be inserted to the database.
3 - after insert Source account record should open

My below code saves the records and redirects correctly but does not fire validation on first name.

Please find controller code below :- 

public class AddMultipleContacts {
  
    public List<Contact> ContactList {get;set;}     
    public Integer rowNum{get;set;}
    public string AcctID{get;set;}
   
       
    sObject con = [Select ID ,FirstName , LastName from Contact LIMIT 1];
    
    public AddMultipleContacts(ApexPages.StandardController controller ){
        AcctID = ApexPages.currentPage().getParameters().get('Id');
        system.debug('-AcctID--'+AcctID);
        ContactList = new List<Contact>(); 
        ContactList.add(new contact(Accountid = Acctid));        
    }        
   
 
    
    public PageReference save(){
            for(Contact con :ContactList) 
            {
            if(con.FirstName == '' || con.FirstName == null)
                
            {
                ApexPages.addmessage(new ApexPages.message(ApexPages.severity.FATAL,'Please enter Contact First name'));
            } else 
                
            {
                insert ContactList ;
            }
            }    
        PageReference page1 = new PageReference ('https://ap4.salesforce.com/'+ AcctId ); 
        Page1.setRedirect(true) ;  
        return page1;        
            } 
          
    public void insertRow(){
        ContactList.add(new contact(Accountid = Acctid ));
        
    }    
    public void delRow(){
        rowNum = Integer.valueOf(apexpages.currentpage().getparameters().get('index'));
        ContactList.remove(rowNum);   
    }        
    public PageReference Cancel(){
     PageReference page2 = new PageReference ('https://ap4.salesforce.com/'+ AcctId )   ;
     Page2.setRedirect(true) ;  
    return page2;
}
    
    
}
Best Answer chosen by Rutvij Pathak 5
Diego Arenas CasadoDiego Arenas Casado
Hi,
You need return null when find the error.

The other error is the insert within a "for"
othe error is in a url put the partial URL not url completed

public PageReference save(){
    for(Contact con :ContactList) {
        if(con.FirstName == '' || con.FirstName == null){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.FATAL,'Please enter Contact First name'));
            return null;
        }
    }
    insert ContactList ;
    PageReference page1 = new PageReference ('/'+ AcctId ); 
    Page1.setRedirect(true);
    return page1;
}

Regards
Diego Arenas
 

All Answers

jane1234jane1234
Hi Rutvij
You can try below logic 


 integer apexflag=0;
          if(con.FirstName == '' || con.FirstName == null)
                
            {
                ApexPages.addmessage(new ApexPages.message(ApexPages.severity.FATAL,'Please enter Contact First name'));
                 apexflag=1;
            } else 
                
            {
                apexflag=0; 
                insert ContactList ;
            }
            if(apexflag==1)
            {
            return null;
            }
Diego Arenas CasadoDiego Arenas Casado
Hi,
You need return null when find the error.

The other error is the insert within a "for"
othe error is in a url put the partial URL not url completed

public PageReference save(){
    for(Contact con :ContactList) {
        if(con.FirstName == '' || con.FirstName == null){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.FATAL,'Please enter Contact First name'));
            return null;
        }
    }
    insert ContactList ;
    PageReference page1 = new PageReference ('/'+ AcctId ); 
    Page1.setRedirect(true);
    return page1;
}

Regards
Diego Arenas
 
This was selected as the best answer