You need to sign in to do that
Don't have an account?
Srujana 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;
}
<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;
}
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.
Server-side it will be received as a String parameter. Deserialize the JSON String to a list of records. 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/
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/ )
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.