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
Abhinav Sharma.Abhinav Sharma. 

update mutiple record on click of button in lightning

Hi All,

My requirementt is to update the multiple records at the same time on the click of a button with the value which is input by the user from front end in lightning component.

Please let me know if any one face the scenario and provide the solution/ implemented code for the same.

Regards,
Abhinav Sharma
sfdcsushilsfdcsushil
I have done that in past. You just need to define list variable. Once user clicks button, button calls the JS controller, JS controller can fetch the latest data from view. You can pass that list directly to your Apex Controller method. 
sfdcsushilsfdcsushil
Actually you dont need to fetch anything, Data is by default available in view for registered variables. 
Abhinav Sharma.Abhinav Sharma.
Hi Sushil,

Can you please provide me the code snippet for the same which you implemented as i got stuck while passing the list from JS controller to Apex Controller.

Regards,
Abhinav
sfdcsushilsfdcsushil
Abhinav,

Unfornunately i dont have that with me. You just need to make sure data type of the list you defined in Apex Controller matches with the one you defined in your view. If you paste snippet of your code, i might be able to help you.

Thanks. 
 
Abhinav Sharma.Abhinav Sharma.
Hi Sushil,

Please find below small app where user can select any account from drop down then in child component associated contacts will get display.
Now the requiremnt is i have to update the email id's of multiple contacts at the same time on click oof save button.

AccountParentComponent.cmp

<aura:component controller="AccountContactClass">   
    <aura:handler name="init" action="{!c.doInit}" value="{!this}"/>  
    <table class="slds-table slds-max-medium-table--stacked">
        <th>                               
            <tr>
                <td class="slds-cell-wrap  slds-text-body--large">
                    <b>Account : </b>
                </td>
               
                <td class="slds-cell-wrap">
                    <ui:inputSelect change="{!c.onAccountChange}" aura:id="selectedAccount" >                                      
                        <aura:iteration items="{!v.lstAccounts}" var="account">
                            <ui:inputSelectOption label="{!account.Name}" text="{!account.Id}" />
                        </aura:iteration>
                    </ui:inputSelect>
                </td>
            </tr>
        </th>       
    </table>
   
    <div>                   
        <c:ChildContactComponent/>                                                                                                         
    </div>
</aura:component>
------------------------------------------------------------------------------
AccountParentComponentController.js

({
    doInit : function(component, event, helper) {      
        var selectedAccount = component.find('selectedAccount').get("v.value");
        component.set("v.SelAcc",selectedAccount);
        //Used to get Accounts to show in Drop down
        var action = component.get("c.getLstAccounts");       
        action.setCallback(this,function(a){
            component.set("v.lstAccounts",a.getReturnValue());
        });      
        $A.enqueueAction(action);
    },
   
    onAccountChange : function(component, event, helper){       
        var selectedAccount = component.find('selectedAccount').get("v.value");
        component.set("v.SelAcc",selectedAccount);
       
        var myEvent = $A.get("e.c:accountContactEvent");
        myEvent.setParams({"selectedAccount" : selectedAccount}).fire();
    },
})
--------------------------------------------------------------------------------
ChildContactComponent.cmp

<aura:component controller="AccountContactClass">
    <link rel="stylesheet" href="/resource/SLDS0101/assets/styles/salesforce-lightning-design-system-vf.css" />
    <link rel="stylesheet" href="/resource/SLDS0101/assets/styles/style.css" />
    <link rel="stylesheet" href="/resource/basicstyle/css/Style2/styles.css" />   
   
    <aura:handler name="init" value="{!this}" action="{!c.doInit}" />
    <aura:handler event="c:accountContactEvent" action="{!c.updateEvent}"/>
   
    <table class="slds-table slds-table--bordered slds-max-medium-table--stacked-horizontal">
        <tr class="slds-text-heading--label">
            <th scope="col">
                <span class="slds-truncate">Name</span>
            </th>
            &nbsp;&nbsp;
            <th scope="col">
                <span class="slds-truncate">Phone</span>
            </th>
            &nbsp;&nbsp;
            <th scope="col">
                <span class="slds-truncate">Email</span>
            </th>
            &nbsp;&nbsp;
            <th scope="col">
                <span class="slds-truncate">Account Name</span>
            </th>
        </tr>
        <tbody>
            <aura:iteration items="{!v.object}" var="obj">
                <tr class="slds-hint-parent">
                    <td data-label="Name">
                        <span class="slds-truncate">
                            <a href="{!'/' + obj.lineId}">
                                <b><ui:outputText value="{!obj.Name}"/></b>
                            </a>
                        </span>
                    </td>
                    &nbsp;
                    &nbsp;
                    <td data-label="Phone" >
                        <span class="slds-truncate">
                            <ui:outputText value="{!obj.Phone}" />
                        </span>
                    </td>
                    &nbsp;
                    &nbsp;
                    <td data-label="Email" >
                        <span class="slds-truncate">
                            <ui:inputText value="{!obj.Email}" />
                        </span>
                    </td>
                    &nbsp;
                    &nbsp;
                    <td data-label="Email" >
                        <span class="slds-truncate">
                            <a href="{!'/' + obj.AccountIDs}">
                                <b><ui:outputText value="{!obj.AccountName}" aura:Id="AccountName"/></b>
                            </a>
                        </span>
                    </td>  
                </tr>
                <tr class="stop"> </tr>
               
            </aura:iteration>
        </tbody>
    </table>
   
    <div>
        <button class="slds-button slds-button--neutral" type="button" onclick="{!c.SaveButton}">
            <span class="slds-truncate">Save</span>
        </button>
    </div>
</aura:component>
-----------------------------------------------------------------------------
ChildContactComponentController.js

({   
    doInit : function(component, event, helper) {      
        var selectedAccount = component.get("v.selectedAccount");
       
        var action = component.get("c.getLstContact");
        action.setParams({
            "accountId" : selectedAccount
        });       
        action.setCallback(this,function(a){
            component.set("v.object",a.getReturnValue());
        });       
        $A.enqueueAction(action);
    },
   
    updateEvent : function(component, event, helper) {
        var selectedAccount = event.getParam("selectedAccount");
       
        var action = component.get("c.getLstContact");
        action.setParams({
            "accountId" : selectedAccount
        });       
        action.setCallback(this,function(a){
            component.set("v.object",a.getReturnValue());
        });       
        $A.enqueueAction(action);
    },
   
    SaveButton : function(component, event, helper) {
        var selectedAccount = component.find("AccountName");      
        var action = component.get("c.getSaveContacts");
        action.setParams({
            "accountName" : selectedAccount[0].get("v.value")
        });       
        action.setCallback(this,function(a){
            component.set("v.object",a.getReturnValue());
        });       
        $A.enqueueAction(action);
    }  
})
--------------------------------------------------------------------------------
AccountContactApp.app

<aura:application >
 <c:AccountParentComponent />
</aura:application>

--------------------------------------------------------------------------------
AccountContactClass.cls

public class AccountContactClass {
 
    @AuraEnabled
    public static List<Account> getLstAccounts() {                      
        return [select id,Name from Account];
    }
   
    @AuraEnabled
    public static List<LineItem> getLstContact(String accountId) {
        List<LineItem> origLineItems = new List<LineItem>();           
        List<Contact> fshList = [select id,Name,Phone,Email,Account.Name,AccountID from contact where AccountID=:accountId];       
        if(fshList !=null){
            for(contact fsh: fshList){ 
                lineItem  li = new LineItem();
                li.lineId = fsh.id;                                                   
                li.Name  = fsh.Name;                               
                li.Phone = fsh.Phone;                
                li.Email = fsh.Email;
                li.AccountName = fsh.Account.Name;
                li.AccountIDs = fsh.AccountID;
                origLineItems.add(li);
            }           
        }
        return origLineItems;
    }
   
    @AuraEnabled
    public static List<LineItem> getSaveContacts(String accountName) {
        List<LineItem> origLineItems = new List<LineItem>();           
        List<Contact> fshList = [select id,Name,Phone,Email,Account.Name,AccountID from contact where Account.Name=:accountName];
        String AccID = fshList[0].AccountID;
        if(fshList !=null){
            for(contact fsh: fshList){ 
                lineItem  li = new LineItem();
                li.lineId = fsh.id;                                                   
                li.Name  = fsh.Name;                               
                li.Phone = fsh.Phone;                
                li.Email = fsh.Email;
                li.AccountName = fsh.Account.Name;
                li.AccountIDs = fsh.AccountID;
                origLineItems.add(li);
            }           
        }

        for (LineItem CU : origLineItems) {
   CU.Save();
  }
       
        return getLstContact(AccID);
    }
   
    public class LineItem{
        @AuraEnabled
        public String lineId {get;set;}
        @AuraEnabled
        public String Name {get;set;}
        @AuraEnabled
        public String Phone {get;set;}
        @AuraEnabled
        public String Email {get;set;}
        @AuraEnabled
        public String AccountName {get;set;}
        @AuraEnabled
        public String AccountIDs {get;set;}
       
        public void Save() {
            Contact c = [SELECT c.Email FROM Contact c WHERE c.ID = :lineId LIMIT 1];
            c.Email = Email;
            update c;
        }
    }
   
}
---------------------------------------------------------------------------------------------

Regards,
admin brkadmin brk
I am getting this error
Failed to save undefined: No EVENT named markup://c:accountContactEvent found : [markup://c:ChildContactComponent]
 
Mahendran Sundarraj 14Mahendran Sundarraj 14

Abhinav,

I have a very similar requirement. Have you got this working? if so please do share the code with me.

Ajesh Juluru 9Ajesh Juluru 9
Hi Abhinav,

If you found any solution for this please post. We are also facing same issue.