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
Zane Prater 15Zane Prater 15 

Could someone help with the lightning button in my controller, getting error: Uncaught Action failed: c:AccConAddressChange$controller$updateCheck11 [c is not defined]?

Simple requirement: Button that checks a boolean field (synch__c) on all contacts.

APEX CLASS
______________________________________________________
public with sharing class AccountContactAddressChange 
{
    @AuraEnabled
    public static list<Contact> getRelatedList(Id recordId)
    {
        
        List<Contact> Conlist = [Select id,firstname,lastname,MailingCity,
                                 MailingState,MailingPostalCode,account.billingaddress, synch__c
                                 from Contact where AccountId =: recordId ];
        return Conlist;
        
    }
    
    @AuraEnabled
    public static void updateRelatedList(List<Contact> Conlist)
    {
        if(Conlist!= null && Conlist.size()>0)
        {
            update Conlist;
        }
    }
    
    @AuraEnabled
    public static void UpdateCheck(List<Contact> Conlist) {
        For(Contact c:Conlist) {
            c.Synch__c=true;
        }

        update Conlist;
        
    }

}
COMPONENT
_______________________________________________________
<aura:component controller = "AccountContactAddressChange" implements="flexipage:availableForRecordHome,force:hasRecordId" access="global" >
    <aura:attribute name="recordId" type="Id" />
    <aura:attribute name="ContactList" type="Contact[]" />
    <aura:attribute name="columns" type="List"/>
    <aura:attribute name="UpdatedList" type="Contact[]"/>
    <aura:handler name="init" value="{!this}" action="{!c.RetrieveData}" />
    <lightning:card iconName="standard:work_capacity_usage" title="Synch Address">
            <div class="slds-align_absolute-center" style="height:5rem"> 
        <button class="slds-button slds-button_brand " onclick="{!c.updateCheck11}">Synch</button></div>
        
            <aura:if isTrue="{!not(empty(v.ContactList))}">
                <lightning:datatable data="{!v.ContactList }" 
                                     columns="{!v.columns }" 
                                     keyField="Id"
                                     draftValues= "{!v.UpdatedList}"
                                     onsave="{!c.SaveUpdatedContacts}"       
                                     hideCheckboxColumn="true"/>
                <aura:set attribute="else">
                    <div Style="text-align : center"> " There are no related contacts "</div>
                </aura:set>
            </aura:if>
        </lightning:card>
    </aura:component>

JS CONTROLLER
_______________________________________________________
({
 RetrieveData : function(component, event, helper) 
    {
         component.set('v.columns', [
            {label: 'First Name', fieldName: 'FirstName', type: 'text'},
            {label: 'Last Name', fieldName: 'LastName', type: 'text'},            
            {label: 'City', fieldName: 'MailingCity', type: 'string'},
            {label: 'State', fieldName: 'MailingState', type: 'string'},
            {label: 'Zipcode', fieldName: 'MailingPostalCode', type: 'string'},
            {label: 'Synch', fieldName: 'Synch__c', type: 'boolean', editable: true} 

         ]);
        var ConList = component.get("c.getRelatedList");
        ConList.setParams
        ({
            recordId: component.get("v.recordId")
        });
        
        ConList.setCallback(this, function(data) 
                           {
                               component.set("v.ContactList", data.getReturnValue());
                           });
        $A.enqueueAction(ConList);
 },
    SaveUpdatedContacts : function(component,event,helper) 
    {
        var UpdatedList = event.getParam('draftValues');        
        var UpdateContacts = component.get("c.updateRelatedList");
        
        UpdateContacts.setParams
        ({
            Conlist : UpdatedList
        });
        UpdateContacts.setCallback(this, function(response) 
                           {
                                var state = response.getState();
                                if (state === 'SUCCESS')
                                {
                                     $A.enqueueAction(component.get('c.RetrieveData'));
      $A.get('e.force:refreshView').fire();
                                }
                                else{
                                     //error handling
                                }
                           });
        $A.enqueueAction(UpdateContacts);
    },

 updateCheck11 : function(component, event, helper)
 {
     helper.updateCheck11_helper(component,event,helper);

     }
 }
)

HELPER
_______________________________________________________

({
    updateCheck11_helper : function(component, event, helper) {
        alert('sdfsd');
        var save_action = c.get("c.updateCheck11");
        save_action.setParams({
        });
        $A.enqueueAction(save_action);
        
    }
})
David Zhu 🔥David Zhu 🔥
It is caused by helper method:
({
    updateCheck11_helper : function(component, event, helper) {
        alert('sdfsd');
        var save_action = component.get("c.updateCheck11");   //this should be a Apex method. I guess it should be c.UpdateCheck
        save_action.setParams({
        });
        $A.enqueueAction(save_action);
        
    }
})
Zane Prater 15Zane Prater 15
Thanks David, I did update the method to 'updateCheck11' so name is consistent for the class, controller, and helper.  I still get the same error however.
David Zhu 🔥David Zhu 🔥
Please don't use the same method name in Apex and JS controller (don't use updateCheck11 in both places). You better use a different name in Apex class.
also make sure it is
var save_action = component.get("c.xxxx");

instead of 

var save_action = c.get("c.xxxx");
Zane Prater 15Zane Prater 15
Thanks David, I made those changes but still get the same error.

CLASS:
@AuraEnabled
    public static void UpdateCheck(List<Contact> Conlist) {
        For(Contact c:Conlist) {
            c.Synch__c=true;
            conlist.add(c);
        }

        update Conlist;
        
    }

COMPONENT:
<button class="slds-button slds-button_brand " onclick="{!c.updateCheck11}">Synch</button></div>

CONTROLLER:
updateCheck11 : function(component, event, helper)
 {
     helper.updateCheck11_helper(component,event,helper);

     }

HELPER:
({
    updateCheck11_helper : function(component, event, helper) {
        alert('sdfsd');
        var save_action = component.get("c.UpdateCheck");
        save_action.setParams({
        });
        $A.enqueueAction(save_action);
        
    }
})

 
David Zhu 🔥David Zhu 🔥
There is nothing could be wrong in the code. :-(
Then it could be cache issue.
Can you try to disable cache for Lightning in Session Settings? or just refresh the browser a couple of times?
Zane Prater 15Zane Prater 15
Yep, that cleared the error but my button is not working.  
Zane Prater 15Zane Prater 15
I have updated my helper function to set the key-value pair as my controller method is passing the Conlist param and so I needed to call that method from my java method.  It's still not updating the records.  Any ideas what is wrong?

({
    updateCheck11_helper : function(component, event, helper) {
        alert('Are you sure you want to update addresses?');
        var save_action = component.get("c.UpdateCheck");
       let params = {};  
       params.Conlist = [];
       save_action.setParams({"Conlist" : component.get("v.UpdatedList")
       });
        $A.enqueueAction(save_action);
        
    }
})