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
Srujana D 7Srujana D 7 

I Have a one requirement to insert multiple records that are arranged in table format ,i want to insert multiple records in Object(Attendance__c).can any one help me.

component:
<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" controller="AttendanceCntrl" >
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
    <aura:attribute name="newAttendance" type="Attendance__c[]" default="{
                                                                         'sobjectType': 'Attendance__c',
                                                                         'Name':'',
                                                                         'attendances__c': false,
                                                                         'Class__c': '',
                                                                         'Date__c': '',
                                                                         'Student_Name__c': '',
                                                                         'Sname__c':''                                        }"/>
    <aura:attribute name="Attendance" type="Attendance__c[]"/>
    <aura:attribute name="student" type="Contact[]"/>
    <aura:attribute name="labels" type="string[]"/>
    <aura:attribute name="clases" type="string"/>
    <aura:attribute name="master" type="string[]"/>
    <aura:attribute name="tables" type="boolean" default="false"/>
    <div style="font-size:1.5em">
        <b>Student Attendance Details</b>
    </div>
    <hr/>
    <div class="slds-p-bottom_large slds-p-left_large" style="width:600px" aura:id="tableIf">
        <form class="slds-form--stacked">
            <div class="slds-form-element">
                <lightning:select aura:id="newclass" name="Class" label="Class" value="{!v.Attendance.Class__c}" onchange="{!c.doSomethingOnClass}" >
                    <option value="">choose one...</option>
                    <aura:iteration items="{!v.labels}" var="l">
                        <option value="{!l}" label="{!l}"></option>
                    </aura:iteration>  </lightning:select> 
            </div></form><br/>
    </div>
    <aura:if isTrue="{!v.tables}">
        <table class="slds-table slds-table--bordered slds-table--striped slds-table--cell-buffer slds-table--fixed-layout" reRender="tableIf">
            <thead>
                <tr class="slds-text-heading--label">
                    <th scope="col"><div class="slds-truncate" title="Name"><b>Student Name</b></div></th>
                    <th scope="col"><div class="slds-truncate" title="Name"><b>SName</b></div></th>
                    <th scope="col"><div class="slds-truncate" title="Attendance"><b>Attendance</b></div></th>
                    <th scope="col"><div class="slds-truncate" title="Name"><b>date</b></div></th>
                </tr>
            </thead>
            <tbody>
                <!-- <aura:iteration items="{!v.Attendance}" var="Att">-->
                <aura:iteration items="{!v.student}" var="stu">
                        <!-- <aura:if isTrue="{!and(Att.Class__c == stu.Class__c,Att.Class__c == v.clases)}">-->
                        <tr>
                    <aura:if isTrue="{!( stu.Class__c == v.clases)}">
                   
                            <td aura:id="masterdata"><div class="slds-truncate" title="Student Name">{!stu.Name}</div></td>
                            <td><div >
                                <lightning:input name="Attendance" value="{!v.newAttendance.Sname__c}"/>
                                </div></td>
                            <td><div >
                                <!-- <lightning:input name="Attendance1" type="checkbox" value="{!v.newAttendance.attendances__c}" />-->
                                <ui:inputCheckbox aura:id="checkbox"  value="{!v.newAttendance.attendances__c}" />
                                </div></td>
                            <td><div >
                                <lightning:input name="date" type="date" value="{!v.newAttendance.Date__c}" placeholder="dd/mm/yyyy" />
                                </div></td> 
                         </aura:if>
                       </tr> 
                    
                </aura:iteration>
            </tbody>
        </table>
        <br/><br/>
        <center>
            <lightning:button label="Save" variant="brand" onclick="{!c.clickNew}"/> 
        </center>
    </aura:if>
</aura:component>

Controller:
({
    doInit : function(component, event, helper) {
       /* var RowItemList = component.get("v.newAttendance");
        RowItemList.push({
            'sobjectType': 'Attendance__c',
            'Name':'',
            'Attendance__c': '',
            'Class__c': '',
            'Date__c': '',
            'Student_Name__c': '',
            'Sname__c':''
        });*/
          helper.CreateClass(component,event,helper);
    },
    doSomethingOnClass : function(component, event, helper) {
          var res = event.getSource().get("v.value");
         component.set("v.clases",res);
         component.set("v.tables",true);
        
    },
    clickNew : function(component, event, helper) {
      var newInstance = component.get("v.newAttendance");  
        alert(JSON.stringify(newInstance));
        var action = component.get("c.executeInsert");
         action.setParams({ 
             newAttendance :  newInstance
         });
    action.setCallback(this, function(response) {
        var state=response.getState();
        alert(state);
    });
    $A.enqueueAction(action); 
    }
        
 })

apex class:

 @AuraEnabled 
    public static void executeInsert(Attendance__c newAttendance) {
    system.debug('newInstance'+newAttendance);
        insert newAttendance;
     
    }    
Anil SomasundaranAnil Somasundaran
Hi,

You can convert the (list of records ) parameter from the client side as a JSON String using JSON.Stringify function.
Please see the code skeleton below.
var instanceList = [{//record instance1},{ record instance 2}]; 
action.setParams({ 
             newAttendance :  JSON.Stringify(instanceList)
});

Server-side it will be received as a String parameter. Deserialize the JSON String to a list of records.
@AuraEnabled 
public static void executeInsert(String jsonString ) {
 List<Attendance__c > fieldList = (List<Attendance__c >) JSON.deserialize(jsonString, 
 List<Attendance__c >.class);
/* do the rest using the record list*/
}
Thanks,
Anil
Please mark this as best answer and upvote if your query has been resolved. Visit my blog to find more about lightning https://techevangel.com/author/anilsomasundaran/ 

 

 
Srujana D 7Srujana D 7
i got error like this,
User-added image
Anil SomasundaranAnil Somasundaran
Hi,

It Seems to be a typo. use JSON.stringify  , Mistakly I was given the capital 'S' in the starting of seeded function.
And, make sure that the paramters in the js function and apex function should be same (name of the parameter). I have give jsonString in the apex function but newAttendance in the js function. Please use jsonString in both the places or the other one.


Thanks,
Anil
Please mark this as best answer and upvote if your query has been resolved. Visit my blog to find more about lightning https://techevangel.com/author/anilsomasundaran/  (https://techevangel.com/author/anilsomasundaran/ )
Srujana D 7Srujana D 7
Hi Anil
Again I Got a Error Like This,
FATAL_ERROR System.JSONException: Malformed JSON: Expected '[' at the beginning of List/Set
can you please Guide How to insert Multiple Records at a time.
Srujana D 7Srujana D 7
I think problem at newAttendance attribute.It takes single Record Instance at a time