+ Start a Discussion
Vishakha SomanVishakha Soman 

Error in $A.getCallback() [helper is not defined]

Hi,
I am facing the following error while deleting account from list :
Error in $A.getCallback() [helper is not defined]
My code is as follows:

Component.cmp
<aura:component controller="TestController "> 
    <!-- Attribute Section -->
    <aura:attribute name="sortDirection" type="String" default="asc" />
    <aura:attribute name="defaultSortDirection" type="String" default="asc" />
    <aura:attribute name="sortedBy" type="String" />
    <aura:attribute name="mycolumns" type="List"/>
    <aura:attribute type="List" name="accountList"/>
    <aura:attribute type="List" name="selectedAccountIds"/>
    
    
    <!-- Handler Section -->
    <aura:handler name="init" value="{!this}" action="{!c.ChangeText}"/>
    <!-- Body -->
     <lightning:button variant="destructive" 
                       label="Delete Account"  
                       title="Delete" 
                       onclick="{!c.deleteAccount}"/>
    <lightning:datatable data="{!v.accountList}"
                         columns="{!v.mycolumns}"
                         keyField="Id"
                         hideCheckboxColumn="false"
                         onrowselection="{!c.getSelectedName}"
                         defaultSortDirection="{!v.defaultSortDirection}"
                         sortedDirection="{!v.sortDirection}"
                         sortedBy="{!v.sortedBy}"
                         onsort="{!c.handleSort}"
                         />  
</aura:component>

==================================================
controller.js

({
    ChangeText : function(component, event, helper) {
        helper.getAccountListHelper(component);
    },
    
    getSelectedName:function(component, event, helper){
        var my_ids = [];
        var selectedRows = event.getParam("selectedRows");
        for(var i=0; i< selectedRows.length; i++){
            // alert('You Selected ID'+selectedRows[i].Id);
            my_ids.push(selectedRows[i].Id);
        }
        component.set("v.selectedAccountIds",my_ids);
    },
    
    handleSort : function(cmp, event, helper) {
        helper.handleSort(cmp,event);
    },
    
    deleteAccount: function(component, event, helper) {
        helper.deleteAccountHelper(component, event, helper);
        }
 })
====================================================
helper.js

({
    getAccountListHelper: function(component) {
        component.set('v.mycolumns',[
            {label: 'Account Name' ,fieldName: 'Name',type: 'text', sortable:true},
            {label: 'Account Type' ,fieldName: 'Type',type: 'text'},
            {label: 'Website' ,fieldName: 'Website',type: 'url'},
            {label: 'Phone' ,fieldName: 'Phone',type: 'Phone'},
            {label: 'Industry' ,fieldName: 'Industry',type: 'text'}
            
        ]); 
        
        var action = component.get("c.getAccListApex");
        action.setCallback(this,function(response){
            var state = response.getState();
            if (state == "SUCCESS")
            {
                console.log("====response====",response.getReturnValue());
                component.set("v.accountList",response.getReturnValue());
            }
            if (state == "Error")
            {
                alert('error');
            }
            
        });
        $A.enqueueAction(action);  
    } ,
    sortBy: function(field, reverse, primer) {
        var key = primer
        ? function(x) {
            return primer(x[field]);
        }
        : function(x) {
            return x[field];
        };
        
        return function(a, b) {
            a = key(a);
            b = key(b);
            return reverse * ((a > b) - (b > a));
        };
    },
    handleSort: function(cmp, event) {
        var sortedBy = event.getParam('fieldName');
        var sortDirection = event.getParam('sortDirection');
        var cloneData = cmp.get("v.accountList");
        cloneData =  cloneData.slice(0);
        cloneData.sort((this.sortBy(sortedBy, sortDirection === 'asc' ? 1 : -1)));
        
        cmp.set('v.accountList', cloneData);
        cmp.set('v.sortDirection', sortDirection);
        cmp.set('v.sortedBy', sortedBy);
    },
    
    deleteAccountHelper: function(component,event,Helper) {
        var action = component.get("c.deleteAccountApex");
        action.setParams({accIds : component.get("v.selectedAccountIds")});
        action.setCallback(this,function(response){
            var state = response.getState();
            if (state == "SUCCESS")
            {
                helper.getAccountListHelper(component);
                alert('Account Deleted Sucessfully');
            }
            if (state == "Error")
            {
                alert('error');
            }
            
        });
        $A.enqueueAction(action);  
        
    }
})

====================================================
ApexClass

public class TestController {
    
    @AuraEnabled
    public static List<Account> getAccListApex(){
        List<Account> acclist = new List<Account>();
        acclist = [Select Id, Name, Type, Website, Phone, Industry from Account];
        return acclist;
        
    }
    
     @AuraEnabled
    public static void deleteAccountApex(List<string> accIds){
        List<Account> acclist = [Select id from Account where id in: accIds];
        delete acclist;
          }
}
Best Answer chosen by Vishakha Soman
Yogendra JangidYogendra Jangid
Hi Vishakha,
JS is case sensitive and camelcase and using variables are no different to this. I see you are using Helper in parameter to function while you use helper in definition. So consider changing helper to Helper or vice versa.
deleteAccountHelper: function(component,event,Helper)

Hope this answers your question. If so please can you mark this as the best answer. Thanks

All Answers

Yogendra JangidYogendra Jangid
Hi Vishakha,
JS is case sensitive and camelcase and using variables are no different to this. I see you are using Helper in parameter to function while you use helper in definition. So consider changing helper to Helper or vice versa.
deleteAccountHelper: function(component,event,Helper)

Hope this answers your question. If so please can you mark this as the best answer. Thanks
This was selected as the best answer
CharuDuttCharuDutt
Hii Vishaka
Try Below Code
deleteAccount: function(component, event, Helper) {
        Helper.deleteAccountHelper(component);
        
        }

 Please Mark This As The Best Answer If It Helps
Thanks You!