0

How do I save many records at once by clicking the Save button? I am not able to make the controller "handleSubmit". Please help me?

<aura:component implements="flexipage:availableForRecordHome,force:hasRecordId">
    
    <aura:attribute name="reloadForm" type="boolean" default="true" />
    <aura:attribute name="listCertification" type="List" default="[1]" />
    
    <lightning:card title="Add your certifications" class="formAddCertification">
        
        <lightning:messages />
        
        <div class="buttonFormAddCert">
            <lightning:buttonIcon iconName="utility:add" variant="brand" value="Add more certification" alternativeText="Add more certification" title="Add more certification" onclick="{!c.handlerAddCert}" />
        </div>
        
        
        <aura:iteration items="{!v.listCertification}" var="item">
            <lightning:recordEditForm aura:id="{!'formAddCertification'+item}" objectApiName="Certification__c" >
                
                <div class="slds-size_6-of-6 slds-p-bottom-small">
                    <label >Developer</label>
                    <lightning:inputField aura:id="field" fieldName="Developer__c" variant="label-hidden"  required="true"/>
                </div>
                
                <div class="demo-only demo-only--sizing slds-grid slds-wrap">
                    
                    <div class="slds-size_4-of-6 slds-p-top--x-small">
                        <label >Certification</label>
                        <lightning:inputField aura:id="field" fieldName="Certification__c" variant="label-hidden"/>
                    </div>
                    
                    <div class="slds-size_2-of-6 slds-p-top--x-small">
                        <label >Certification Date</label>
                        <lightning:inputField aura:id="field" fieldName="Certification_Date__c" variant="label-hidden"/>
                    </div>
                    
                </div>
                
                
            </lightning:recordEditForm>            
        </aura:iteration>
        
        <div class="buttonForm">
            <lightning:button label="Cancel" onclick="{!c.handleReset}" />
            <lightning:button type="submit" variant="brand" label="Save" onclick="{!c.handleSubmit}"/>  
        </div>
        
    </lightning:card>
</aura:component>
({
    handlerAddCert: function(component, event, helper) {
        var listCertification = component.get('v.listCertification');
        
        listCertification.push(listCertification.length+1);
        
        component.set('v.listCertification', listCertification);
    },
    
    
    handleReset: function(component, event, helper) {
        component.find('field').forEach(function(f) {
            f.reset();
        });
    },
    
    
    handleSubmit : function(component, event, helper) {
        
        var listCertification = component.find('v.listCertification');
        
        
    }
    
    
})
1

aura:id cannot be an expression, it must be a literal value.

aura:id="formAddCertification"

You can try to submit all the records at once:

component.find("formAddCertification")
  .forEach(function(form) {
    form.submit();
  });

However, this may not work well if there's a large number of records. It is strongly recommended that you use Apex to save multiple records quickly. See this answer for what you can expect with performance (it deals with loads, not saves, but the effect is similar).

3
  • After making the suggested change, I have the following error: " This page has an error. You might just need to refresh it. Action failed: c:AddCertification$controller$handleSubmit [Cannot read property 'forEach' of undefined] Failing descriptor: {c:AddCertification$controller$handleSubmit} " Could you help me again? – Juliana Aug 19 '20 at 3:16
  • @Juliana make sure the aura:id matches the value used for component.find(). – sfdcfox Aug 19 '20 at 3:27
  • That was the cause of the error. Thank you. – Juliana Aug 19 '20 at 3:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.