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
NetmasterNetmaster 

System.DmlException: Insert failed

Hello,

 

I saved this code to an apex class, but when I want to utilise it I  get an error :

 

public class insererEnfant{

    public List<Enfants__c> accts {get; set;}
    
    public insererEnfant(){
        accts = new List<Enfants__c>();
        accts.add(new Enfants__c());
    }
    
    public void addrow(){
        accts.add(new Enfants__c());
    }
    
    public PageReference save(){
        insert accts;
        PageReference home = new PageReference('/home/home.jsp');
        home.setRedirect(true);
        return home;
    }
}

 The error :

System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Assuré]: [Assuré] 
Class.insEnfant.save: line 15, column 1

 How can solve this problem, please ?

ShaTShaT

HI ,

 i think when you are inserting  accts you are missing a required field Assure. 

 if you are using accts in your visualforce page Assure filed should be there to assign value.

so you should assigen a value to the filed first and then insert.

 

Thanks

Shailu

NetmasterNetmaster

Thanks,

 

This is my visaulforce page, I don't find the problem.

 

<apex:page controller="insererEnfant">
    <apex:form >
        <apex:pageBlock >
        
        
            <apex:pageBlockButtons >
                <apex:commandButton value="Save" action="{!save}" rerender="error"/>
            </apex:pageBlockButtons>
                       <apex:pageBlockTable value="{!accts}" var="a" id="table">
                <apex:facet name="footer">
                    <apex:commandLink value="Ajouter" action="{!addRow}" rerender="table,error"/>
                </apex:facet>
                <apex:column headerValue="Nom">
                    <apex:inputField value="{!a.Name}"/>
                </apex:column>
                <apex:column headerValue="Prénom">
                    <apex:inputField value="{!a.Prenom__c}"/>
                </apex:column> 
                                <apex:column headerValue="Né le">
                    <apex:inputField value="{!a.Date_de_naissance__c}"/>
                </apex:column>   
                                <apex:column headerValue="Lieu de naissance">
                    <apex:inputField value="{!a.Lieu_de_naissance__c}"/>
                </apex:column>   
                                <apex:column headerValue="Situation">
                    <apex:inputField value="{!a.Situation__c }"/>
                </apex:column>                          
            </apex:pageBlockTable>
            
        </apex:pageBlock>
    </apex:form>
</apex:page>

 

aballardaballard

Your custom object enfants__c must have been defined with assure as a required field.   And you are not setting it. 

Either your controller needs to set it directly, or the page needs an inputField that is bound to it so the user can specifiy it.