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
Sonam PatilSonam Patil 

Visualforce Exception Error

Hi,
In the below, there are two pageblock, in first block is to display the list of Account and second block is to insert the account.And inserted account in second block should be dynamicaly displayed in 1st block. when I click on Save button it's showing the below error.
System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Account Name]: [Account Name]
 
Please can anyone help me to solve this error.

<apex:page controller="multiAccountInsert">
    <apex:form >
        <apex:pageBlock >
         
            <apex:pageBlockTable value="{!accts}" var="a" reRender="display" >
                  <apex:column value="{!a.Name}"/>  
                  <apex:column value="{!a.Industry}"/>
                  <apex:column value="{!a.phone}"/>             
            </apex:pageBlockTable>
     </apex:pageBlock>
         <apex:pageBlock >
              <apex:pageBlockButtons location="top" >
                <apex:commandButton value="Save" action="{!Save}" id="display"/>
            </apex:pageBlockButtons>
                <apex:pageblocksection >
                    Enter Name: <apex:inputText value="{!at.Name}"/>
                </apex:pageblocksection>
         </apex:pageBlock>
    </apex:form>
</apex:page>
------------------------------------------
public class multiAccountInsert{

    public Account at { get; set; }

    public PageReference Save() {
    at = new Account();
    insert at;
        return null;
    }


    public List<Account> accts {get; set;}
        
        public multiAccountInsert(){
        accts = new List<Account>();
        accts = [Select id, Name, Industry, Phone from Account];
       }
}
Best Answer chosen by Sonam Patil
karthikeyan perumalkarthikeyan perumal
Hello, 

In your controller you did't assign Account name from page while insert. its Required field for Account Insert. 

use below controller and page. 

Controller: 
 
public class multiAccountInsert{

    public Account at { get; set; }
    public String AccountName{get;set;}

    public PageReference Save() {
    at = new Account();
    at.Name=AccountName;
    
    insert at;
    
        return null;
    }


    public List<Account> accts {get; set;}
        
        public multiAccountInsert(){
        accts = new List<Account>();
        accts = [Select id, Name, Industry, Phone from Account];
       }
}

page: 
 
<apex:page controller="multiAccountInsert">
    <apex:form >
        <apex:pageBlock >
         
            <apex:pageBlockTable value="{!accts}" var="a" >
                  <apex:column value="{!a.Name}"/>  
                  <apex:column value="{!a.Industry}"/>
                  <apex:column value="{!a.phone}"/>             
            </apex:pageBlockTable>
     </apex:pageBlock>
         <apex:pageBlock >
              <apex:pageBlockButtons location="Bottom" >
                <apex:commandButton value="Save" action="{!Save}" id="display" />
            </apex:pageBlockButtons>
                <apex:pageblocksection >
                    Enter Name: <apex:inputText value="{!AccountName}"/>
                </apex:pageblocksection>
         </apex:pageBlock>
    </apex:form>
</apex:page>

Hope this will work. 

mark Best ASNWER if its works for you. 

Thanks
karthik

 

All Answers

karthikeyan perumalkarthikeyan perumal
Hello, 

In your controller you did't assign Account name from page while insert. its Required field for Account Insert. 

use below controller and page. 

Controller: 
 
public class multiAccountInsert{

    public Account at { get; set; }
    public String AccountName{get;set;}

    public PageReference Save() {
    at = new Account();
    at.Name=AccountName;
    
    insert at;
    
        return null;
    }


    public List<Account> accts {get; set;}
        
        public multiAccountInsert(){
        accts = new List<Account>();
        accts = [Select id, Name, Industry, Phone from Account];
       }
}

page: 
 
<apex:page controller="multiAccountInsert">
    <apex:form >
        <apex:pageBlock >
         
            <apex:pageBlockTable value="{!accts}" var="a" >
                  <apex:column value="{!a.Name}"/>  
                  <apex:column value="{!a.Industry}"/>
                  <apex:column value="{!a.phone}"/>             
            </apex:pageBlockTable>
     </apex:pageBlock>
         <apex:pageBlock >
              <apex:pageBlockButtons location="Bottom" >
                <apex:commandButton value="Save" action="{!Save}" id="display" />
            </apex:pageBlockButtons>
                <apex:pageblocksection >
                    Enter Name: <apex:inputText value="{!AccountName}"/>
                </apex:pageblocksection>
         </apex:pageBlock>
    </apex:form>
</apex:page>

Hope this will work. 

mark Best ASNWER if its works for you. 

Thanks
karthik

 
This was selected as the best answer
Sonam PatilSonam Patil
Thanks, Karthik, it was helpful for me.