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
DeepikareddyDeepikareddy 

hi , created a Account list , 1st record is not adding to the list

hi , created a list and going to add the record , but the 1st record is not getting added, only the second record is geting add, my i knw the reason behind that thanks ..
<apex:page controller="accountinsert" >
 
 
  <apex:form >
   
   <apex:pageblock >
    <apex:pageblockSection >
     
     <apex:inputField value="{!acc.name}"/>
    <apex:inputField value="{!acc.phone}"/>
    <apex:inputField value="{!acc.Rating}"/>
    <apex:inputField value="{!acc.Industry}"/>
    
    </apex:pageblockSection>
   
    <apex:pageblockButtons location="top">
    
    <apex:commandButton Value="Submit" action="{!addtolist}"  />
    
     <apex:commandButton Value="Clear" action="{!Clear}"  />
    </apex:pageblockButtons>
   
   <apex:pageblockSection columns="1" Rendered="{!lstaccount.size>0}"  >
   
     <apex:pageblockTable value="{!lstaccount}" var="a"  > 
    
     <apex:column headerValue="Name">{!a.name}</apex:column>
     
     <apex:column headerValue="Phone">{!a.Phone}</apex:column>
     
     <apex:column headerValue="Rating">{!a.Rating}</apex:column>
     
     
     <apex:column headerValue="industry">{!a.industry}</apex:column>
    </apex:pageblockTable>
    
    
   </apex:pageblockSection>
  
   
   
   
   
   </apex:pageblock>
  
  </apex:form>
</apex:page>

Controller:
 
public class accountinsert {

 public list<account> lstaccount{get;set;}
 
  public account acc{get;set;}
  
  
   public accountinsert(){
   
    lstaccount= new list<account>();
     //acc = new account();
  
   }
   
    public void addtolist(){
    
     acc = new account();
      
    lstaccount.add(acc);
    
    
    }
    
    public void Clear(){
    
    lstaccount.clear();
    
    }
}

my i know the reason, why iam not getting add, the 1st record, and other getting added,,

Thanks 
Deepika


 
Best Answer chosen by Deepikareddy
Abhishek BansalAbhishek Bansal
Hi Deepika,

Please use the below controller class:
public class accountinsert {

 public list<account> lstaccount{get;set;}
 
  public account acc{get;set;}
  
  
   public accountinsert(){
   
    lstaccount= new list<account>();
     acc = new account();
  
   }
   
    public void addtolist(){
    
    Account newAcc = new account();
    newAcc.Name = acc.Name;
	newAcc.Phone = acc.Phone;
	newAcc.Rating = acc.Rating;
	newAcc.Industry = acc.Industry;
    lstaccount.add(newAcc);
    
    
    }
    
    public void Clear(){
    
    lstaccount.clear();
    
    }
}

Let me know if you still face any issues.

Thanks,
Abhishek Bansal.

All Answers

Abhishek BansalAbhishek Bansal
Hi Deepika,

Please use the below controller class:
public class accountinsert {

 public list<account> lstaccount{get;set;}
 
  public account acc{get;set;}
  
  
   public accountinsert(){
   
    lstaccount= new list<account>();
     acc = new account();
  
   }
   
    public void addtolist(){
    
    Account newAcc = new account();
    newAcc.Name = acc.Name;
	newAcc.Phone = acc.Phone;
	newAcc.Rating = acc.Rating;
	newAcc.Industry = acc.Industry;
    lstaccount.add(newAcc);
    
    
    }
    
    public void Clear(){
    
    lstaccount.clear();
    
    }
}

Let me know if you still face any issues.

Thanks,
Abhishek Bansal.
This was selected as the best answer
DeepikareddyDeepikareddy
yeah got the point ...Its worked, thank you .. Abhishek.. i appreciate your Support .. MArked as a BEst Answer
Thanks
Deepika