I want to create contact record using existing account Id in Aura lightning table ? and I want save a row separately in on submit button it perform bulk insert ?
the component look like this
Component Code:-
<aura:component controller="AddDelRowDynamic"
implements="force:appHostable,flexipage:availableForAllPageTypes,
flexipage:availableForRecordHome,
force:hasRecordId,forceCommunity:availableFo
rAllPageTypes,force:lightningQuickAction" access="global" >
<!--Aura Attribute Start-->
<aura:attribute name="contactList" type="Contact[]"/>
<aura:attribute name="showTableFlag" type="boolean"
default="false"></aura:attribute>
<aura:attribute name="account" type="Account"/>
<aura:attribute name="recordId" type="Id"/>
<!--Aura Attribute End-->
<aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
<lightning:card>
<div class="slds-m-around--xx-large">
<h1 class="slds-page-header__title">Add Row
<lightning:buttonIcon
iconName="utility:add"
size="large"
variant="bare"
alternativeText="Add"
onclick="{!c.addRowController}"/>
</h1>
<!--Table Start-->
<aura:if isTrue="{!v.showTableFlag}">
<div class="container-fluid">
<table class="slds-table slds-table_bordered slds-table_cell-buffer">
<thead>
<tr class="slds-text-title_caps">
<th scope="col">
<div class="slds-truncate">Sr. No</div>
</th>
<th scope="col">
<div class="slds-truncate" title="First Name">First Name</div>
</th>
<th scope="col">
<div class="slds-truncate" title="Last Name">Last Name</div>
</th>
<th scope="col">
<div class="slds-truncate" title="Account Name">Account Name</div>
</th>
<th scope="col">
<div class="slds-truncate" title="Action">Action</div>
</th>
</tr>
</thead>
<tbody>
<aura:iteration items="{!v.contactList}" var="objCon" indexVar="index">
<tr>
<td>
{!index + 1}
</td>
<td>
<lightning:input name="FirstName" type="text" maxlength="50" value="{!objCon.FirstName}" />
</td>
<td>
<lightning:input name="LastName" required="true" type="text" maxlength="50" value="{!objCon.LastName}" />
</td>
<td>
<lightning:input name="Account Name" label="Account Name" value="{!v.account.Name}" disabled="true"/>
</td>
<td>
<a onclick="{!c.deleteRecordController}" data-record="{!index}">
<lightning:icon
iconName="utility:delete"
size="small"
alternativeText="Delete"/>
<span class="slds-assistive-text">Delete</span>
</a>
</td>
</tr>
</aura:iteration>
</tbody>
</table>
<div class="slds-align_absolute-center slds-p-top_small">
<lightning:button
variant="brand"
label="Submit"
title="Brand action"
onclick="{!c.saveContactsController}" />
</div>
</div>
</aura:if>
<!--Table End-->
</div>
</lightning:card>
</aura:component>
Controller Code:-
({
addRowController : function(component, event, helper) {
//get the contact List from component
var contactList = component.get("v.contactList");
//Add New Contact Record
contactList.push({
'sobjectType': 'Contact',
'Name': '',
'Phone': '',
'Fax': '',
'Website ': '',
});
component.set("v.contactList", contactList);
component.set("v.showTableFlag", true);
},
deleteRecordController : function(component, event, helper) {
//Get the contact list
var contactList = component.get("v.contactList");
//Get the target object
var selectedItem = event.currentTarget;
//Get the selected item index
var index = selectedItem.dataset.record;
//Remove single record from contact list
contactList.splice(index, 1);
//Set modified contact list
component.set("v.contactList", contactList);
},
doInit : function(component, event, helper){
var accid = component.get("v.recordId");
component.set("v.contact.AccountId",component.get("v.recordId"));
var action = component.get('c.getAccount');
action.setParams({
acid : component.get("v.recordId")
});
action.setCallback(this, function(response){
if (response.getState() === "SUCCESS"){
console.log('Received
Data'+JSON.stringify(response.getReturnValue()));
component.set("v.account",response.getReturnValue());
}else{
console.log('Something went wrong');
}
});
$A.enqueueAction(action);
},
saveContactsController : function(component, event, helper) {
if (helper.validateContactRecords(component, event)) {
//Call Apex method and pass contact list as a parameters
var action = component.get("c.insertCon");
action.setParams({"newcon": component.get("v.contactList")});
action.setCallback(this, function(response) {
//get response status
var state = response.getState();
alert(state);
if (state === "SUCCESS") {
alert('y')
var contactid=response.getReturnValue().Id;
alert("contact created.Id:"+contactid)
var navEvt = $A.get("e.force:navigateToSObject");
navEvt.setParams({
"recordId": contactid,
"slideDevName": "related"
});
navEvt.fire();
component.set("v.contactList", []);
alert('Contacts saved successfully');
component.set("v.showTableFlag", true);
}
});
$A.enqueueAction(action);
}
}
})
Helper Code:-
({
validateContactRecords: function(component, event) {
//Validate all contact records
var isValid = true;
var contactList = component.get("v.contactList");
for (var i = 0; i < contactList.length; i++) {
if (contactList[i].LastName == '') {
isValid = false;
alert('Contact LastName cannot be blank on '+(i + 1)+'
row number');
}
}
return isValid;
},
})
Apec Class:-
public class AddDelRowDynamic {
@AuraEnabled
public static void saveAccountList(List<Account> accList){
insert accList;
}
@AuraEnabled
public static contact insertCon(Contact newcon)
{
insert newcon;
return newcon;
}
@AuraEnabled
public static Account getAccount(string acid)
{
Account acc = [select id,name from Account where Id =:acid];
return acc;
}
}
I want to create contact record using existing account Id in Aura lightning table ? and I want save a row separately in on submit button it perform bulk insert ?