+ Start a Discussion
Ramakrishna KRamakrishna K 

How to pass two parameters to serverside controller using "onrowaction" lightning datatable

Hi All ,
i'm trying to update a record using  "onrowaction " button in lightning datatable , when i'm passing the parameters to server side controller getting this error  "Cannot read property 'updaterecord' of undefined".

Please help me to find solutions for below querrys 
1. How to capture row id and pass to server side controller .
2. How to get the component value ( getting error 'Cannot read property 'updaterecord' of undefined')
2. Is it possible to call helper method using "onrowaction " . Below my sample code

Controller :

component.set('v.columns', [
            {label: $A.get("$Label.c.AccountName"), fieldName: 'Name', type: 'text', sortable : true},
            {label: 'Update', type: 'button', initialWidth: 135, typeAttributes: { label: 'Update', name: 'view_details', title: 'Click to View Details'}}]);

handleRowAction :function(component, event, helper){
       var action = event.getParam('action');
       var row = event.getParam('row');
       switch (action.name) {
           default:
               helper.showRowDetails(row);
               break;
       }

Helper :

showRowDetails : function(row ,component, event,helper ) {
       // var kkk = component.get("v.AccountID");
       alert("Showing opportunity " + row.Name + " closing on " + row.Id);
        helper.updaterecord(component, event,helper);
   },
    
    updaterecord : function(cmp, event,helper ) {
       var action = cmp.get("c.updaterecord"); 
      action.setParams({"focID " : cmp.get("v.kkrecordId")}); 
        action.setCallback(this, function(response) {
            var state = response.getState();
            var responseval = response.getReturnValue();

            if (cmp.isValid() && state === "SUCCESS" && responseval.isSuccess) {
                console.log(response.getReturnValue());
                   window.location.replace("/"+cmp.get("v.kkrecordId"));
            }
            else if(state == "ERROR" || !responseval.isSuccess){
                alert(responseval.message);
            }
        });

Component :

<lightning:datatable data="{! v.allacountlist}" class="slds-m-top_medium"
                             columns="{! v.columns }" 
                             keyField="id"
                             aura:id="tableid"
                             hideCheckboxColumn="true"
                             onrowaction="{! c.handleRowAction }"/>
Raj VakatiRaj Vakati
Refer this code
 
@AuraEnabled
    public static void deleteBooks(String ids ){
        Delete [Select id from Books__c where  id=:ids];
    } 
	
	
	var actions = [
            { label: 'Show details', name: 'show_details' },
            { label: 'Delete', name: 'delete' }
        ];
		
		            { type: 'action', typeAttributes: { rowActions: actions } }
					

		
		
	
	
	
	onheaderaction="{! c.handleHeaderAction }"


	
	handleRowAction: function (cmp, event, helper) {
        var action = event.getParam('action');
        var row = event.getParam('row');
        switch (action.name) {
            case 'show_details':
                var navEvt = $A.get("e.force:navigateToSObject");
                navEvt.setParams({
                    "recordId": row.Id,
                    "slideDevName": "detail"
                });
                navEvt.fire();
                break;
            case 'delete':
                var rows = cmp.get('v.data');
                var rowIndex = rows.indexOf(row);
                console.log('rowIndex'+rowIndex);
                console.log('rowIndex row'+rows[rowIndex].Id);
                var deleteAct = cmp.get("c.deleteBooks");
                deleteAct.setParams({ ids : rows[rowIndex].Id });
                $A.enqueueAction(deleteAct);
                var toastEvent = $A.get("e.force:showToast");
                toastEvent.setParams({
                    "title": "Success!",
                    "message": "The record has been delete successfully."
                });
                toastEvent.fire();
                rows.splice(rowIndex, 1);
                cmp.set('v.data', rows);
                break;
        }
    },

 
Raj VakatiRaj Vakati
https://rajvakati.com/2018/02/18/usage-of-lightningdatatable/
Ramakrishna KRamakrishna K
Thanks Raj V ,
Here problem is i want capture row.Id and pass to controller , when i set the value to attribute getting  Cannot read property error message .
 component.set("v.accid" , row.id);
Raj VakatiRaj Vakati
Can you share complete code from apex controller and component 
Ramakrishna KRamakrishna K
I want to pass row id to Accid 

class :
public static Account  updaterecord(ID CusId , ID Accid) {
         Account objacc = [select id,Sold_to_customer__c from Account  where id=:CusId ]; 
         objacc.Sold_to_customer__c = Accid ;
          try{ 
           update objacc;
           }
           catch(exception e){}
           return objacc ;
        }


updaterecord : function(row ,cmp, event,helper ) {
       var action = cmp.get("c.updaterecord"); 
      
      action.setParams({"CusId " : cmp.get("v.kkrecordId") ,"Accid" : 'here i want to pass row id'}); 
        action.setCallback(this, function(response) {
            var state = response.getState();
            var responseval = response.getReturnValue();

            if (cmp.isValid() && state === "SUCCESS" && responseval.isSuccess) {
                console.log(response.getReturnValue());
                   window.location.replace("/"+cmp.get("v.kkrecordId"));
            }
            else if(state == "ERROR" || !responseval.isSuccess){
                alert(responseval.message);
            }
        });


component.set('v.columns', [
            {label: $A.get("$Label.c.AccountName"), fieldName: 'Name', type: 'text', sortable : true},
            {label: 'Update', type: 'button', initialWidth: 135, typeAttributes: { label: 'Update', name: 'view_details', title: 'Click to View Details'}}]);

handleRowAction :function(component, event, helper){
       var action = event.getParam('action');
       var row = event.getParam('row');
       switch (action.name) {
           default:
               helper.updaterecord (row,component, event, helper);
               break;
       }


<lightning:datatable data="{! v.allacountlist}" class="slds-m-top_medium"
                             columns="{! v.columns }" 
                             keyField="id"
                             aura:id="tableid"
                             hideCheckboxColumn="true"
                             onrowaction="{! c.handleRowAction }"/>
Raj VakatiRaj Vakati
try this code
 
updaterecord : function(row ,cmp, event,helper ) {
       var action = cmp.get("c.updaterecord"); 
       var rows = cmp.get('v.data');
         var rowIndex = rows.indexOf(row);
         console.log('rowIndex'+rowIndex);
         console.log('rowIndex row'+rows[rowIndex].Id);
         action.setParams({"CusId " : cmp.get("v.kkrecordId") ,"Accid" :  rows[rowIndex].Id}); 
        action.setCallback(this, function(response) {
            var state = response.getState();
            var responseval = response.getReturnValue();

            if (cmp.isValid() && state === "SUCCESS" && responseval.isSuccess) {
                console.log(response.getReturnValue());
                   window.location.replace("/"+cmp.get("v.kkrecordId"));
            }
            else if(state == "ERROR" || !responseval.isSuccess){
                alert(responseval.message);
            }
        });

 
Ramakrishna KRamakrishna K
it seems get not working in onrowaction
tried multiple ways but getting below errors 

Cannot read property 'get' of undefined
Cannot read property 'indexOf' of undefined

 
Raj VakatiRaj Vakati
Change 
 from

 
var rows = cmp.get('v.data');

TO 
var rows = cmp.get('v.allacountlist');

 
Ramakrishna KRamakrishna K
No luck   :(
Cannot read property 'get' of undefined
Ashwin Kumar SrinivasanAshwin Kumar Srinivasan
Hi Ramakrishna,

Try using different names for the controller method in javascript and your apex method name (both are named as updaterecord.
"Use unique names for client-side and server-side actions in a component. A JavaScript function (client-side action) with the same name as an Apex method (server-side action ) can lead to hard-to-debug issues. (https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/controllers_server_actions_call.htm)" 

and if that does not help at the point you are accessing var row = event.getParam('row'); if you console out row.Id do you have the value? before you set it for the server side param?

Thanks,
Ashwin
Edgars Everts 5Edgars Everts 5

Hi,

According to: https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/js_helper.htm

({
    helperMethod1 : function() {
        // logic here
    },

    helperMethod2 : function(component) {
        // logic here
        this.helperMethod3(var1, var2);
    },

     helperMethod3 : function(var1, var2) {
         // do something with var1 and var2 here
    }
})

since that you should fix helper method call in helper ( helpers usually do not have parameter helper so remove : " ,helper"    ) , and rest change commented in code below.

Helper :

   showRowDetails : function(component, event, row ) {
       // var kkk = component.get("v.AccountID");
       alert("Showing opportunity " + row.Name + " closing on " + row.Id);
       this.updaterecord(component, event, row);
   },
    
/** remove ",helper" as haven't seen helper use in helper class itself 
*  add ",row" as still parameter could be needed in next function?
*/
   updaterecord : function(cmp, event, row ) {
       var action = cmp.get("c.updaterecord"); 
/** you can set many parameters as follow, param names should be the same caseSensitive as in server-apexClass */
       action.setParams({
              "focID" : cmp.get("v.kkrecordId"),
              "nameOfSecondParameter" : variableOfSecondParameter
              }); 

       action.setCallback(this, function(response) {
           var state = response.getState();
           var responseval = response.getReturnValue();

           if (cmp.isValid() && state === "SUCCESS" && responseval.isSuccess) {
               console.log(response.getReturnValue());
       // not sure is window.location is needed here?
       // window.location.replace("/"+cmp.get("v.kkrecordId"));

           } else if(state == "ERROR" || !responseval.isSuccess){

               alert(responseval.message);
           }
       }
/** missing Action execution */
       $A.enqueueAction(action); 
}
        
});
Not sure if ithat helps to solve the issue, but at least that's what should be changed first.
Enjoy IT ;)