Apex Class
public class ContactsaddToAccountController {
@AuraEnabled
public static List<contact> getContacts() {
List<contact> totalList = new List<contact>();
for(Contact cc : [SELECT Id,Name,Email from contact limit 20]){
totalList.add(cc);
}
return totalList;
}
@AuraEnabled
public static void addParentAccount(String ParentId , List<String> lstOfContactIds){
list<Contact> lstContacts = new list<Contact>();
for(string sContactId : lstOfContactIds){
Contact oContact = new contact();
oContact.Id = sContactId;
oContact.AccountId = ParentId;
lstContacts.add(oContact);
}
update lstContacts;
}
}
Lightning Component
<aura:component controller="ContactsaddToAccountController" Implements="flexipage:availableForRecordHome,force:hasRecordId">
<aura:handler name="init" value="{!this}" action="{!c.doInit}" />
<aura:attribute name="ChildRecordList" type="List" />
<div class="slds-m-around--x-small" style="background:white;">
<div class="slds-grid slds-grid--align-end slds-m-around--x-small slds-align_absolute-center">
<button class="slds-button slds-button--brand" onclick="{!c.addSelected}">Add Records</button>
</div>
<table class="slds-table slds-table--bordered slds-table--striped slds-table--cell-buffer slds-table--fixed-layout">
<thead>
<tr class="slds-text-heading--label">
<th scope="col">
<div class="slds-truncate" title="Name">Name</div>
</th>
<th scope="col">
<div class="slds-truncate" title="Email">Email</div>
</th>
<th scope="col">
<div class="slds-truncate" title="Select">Select</div>
</th>
</tr>
</thead>
<tbody>
<aura:iteration items="{!v.ChildRecordList}" var="rec">
<tr>
<td>
<div class="slds-truncate" title="{!rec.Name}">{!rec.Name}</div>
</td>
<td>
<div class="slds-truncate" title="{!rec.Email}">{!rec.Email}</div>
</td>
<td>
<ui:inputCheckbox aura:id="checkBox" text="{!rec.Id}" />
</td>
</tr>
</aura:iteration>
</tbody>
</table>
</div>
</aura:component>
JS Controller
({
doInit: function(component, event, helper) {
helper.getChildRecords(component, event);
},
addSelected: function(component, event, helper) {
var tempIDs = [];
var getAllId = component.find("checkBox");
for (var i = 0; i < getAllId.length; i++) {
if (getAllId[i].get("v.value") == true) {
tempIDs.push(getAllId[i].get("v.text"));
}
}
helper.addSelectedHelper(component, event, tempIDs);
},
})
JS Helper
({
getChildRecords: function(component, event) {
var action = component.get('c.getContacts');
action.setCallback(this, function(actionResult) {
var state = actionResult.getState();
if (state === 'SUCCESS') {
component.set('v.ChildRecordList', actionResult.getReturnValue());
}
});
$A.enqueueAction(action);
},
addSelectedHelper: function(component, event, childRecordsIds) {
var action = component.get('c.addParentAccount');
action.setParams({
"ParentId": component.get("v.recordId"),
"lstOfContactIds": childRecordsIds
});
action.setCallback(this, function(response) {
var state = response.getState();
if (state === "SUCCESS") {
// This standard 'e.force:refreshView' and 'e.force:showToast' event not work from lightning appliation
var toastEvent = $A.get("e.force:showToast");
toastEvent.setParams({
"title": "Success!",
"message": "The Child record's has been added successfully."
});
toastEvent.fire();
$A.get('e.force:refreshView').fire();
this.getChildRecors(component,event);
}
});
$A.enqueueAction(action);
},
})
Output