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
smita bhargavasmita bhargava 

issue in using base utility helper method

Hello
I am trying to understand how to use base utility helper method in lighting component.
Base.cmp
--------
<aura:component abstract="true">
	{!v.body}
</aura:component>


BaseHelper.js
--------------
({
    callServer : function(component,method,callback,params) {
        var action = component.get(method);
        if (params) {
            action.setParams(params);
        }
       
        action.setCallback(this,function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                // pass returned value to callback function
                callback.call(this,response.getReturnValue());  
            } else if (state === "ERROR") {
                // generic error handler
                var errors = response.getError();
                if (errors) {
                    console.log("Errors", errors);
                    if (errors[0] && errors[0].message) {
                        throw new Error("Error" + errors[0].message);
                    }
                } else {
                    throw new Error("Unknown Error");
                }
            }
        });
       
        $A.enqueueAction(action);
    }
})


ApexController.axpc
-------------------
public with sharing class ApexController {
    @AuraEnabled
    public static List<Contact> getContacts() {
        List<Contact> contacts = [select Id, Name, MailingStreet, Phone, Email FROM Contact];
        return contacts;
    }
}


CallApexClassCmpBase.cmp
------------------------

<aura:component controller="ApexController">
	
    <lightning:card title="Bring Contact data">
       
          <aura:set attribute="actions">
       
              <lightning:button label="Contact Data" onclick="{!c.Condata}" />
          </aura:set> 
   
    </lightning:card>
    
</aura:component>


In the controller.js of the above component

({
 Condata : function(component,event,helper) {
 helper.callServer(
            component,
            "c.getContacts",
            function(response) { 
               console.log('Contacts data'+JSON.stringify(response));
            }
        );
      
    }
})



<aura:application extends="force:slds">

    <c:CallApexClassCmpBase/>
     
 </aura:application>
when I preview this I am getting an error as below:

This page has an error. You might just need to refresh it. Action failed: etaPrime:CallApexclassCmpBase$controller$Condata [helper.callServer is not a function] Failing descriptor: {etaPrime:CallApexclassCmpBase$controller$Condata}

I am not able to figure out what is causing the issue,

Please help.

smita
Best Answer chosen by smita bhargava
Shivdeep KumarShivdeep Kumar
Hi Smita,

Try below code and let me know if this help !
 
Base.cmp
--------
<aura:component abstract="true">
	{!v.body}
</aura:component>


BaseHelper.js
--------------
({
    callServer : function(component,method,attributeName,params) {
        var action = component.get(method);
        if (params) {
            action.setParams(params);
        }
       
        action.setCallback(this,function(response){
            var state = response.getState();
        	console.log('State log '+state);
            if(state === 'SUCCESS'){
                component.set(attributeName,response.getReturnValue());
            }else if(state === 'ERROR'){
		console.log('Error '+response.getError());
	}

        });
        $A.enqueueAction(action);
    }
})


ApexController.axpc
-------------------
public with sharing class ApexController {
    @AuraEnabled
    public static List<Contact> getContacts() {
        List<Contact> contacts = [select Id, Name, FirstName,LastName,MailingStreet, Phone, Email FROM Contact];
        return contacts;
    }
}


CallApexClassCmpBase.cmp
------------------------

<aura:component controller="ApexController" extends="c:Base">
	<aura:attribute name="Contact" type="List"/>
    <lightning:card title="Bring Contact data">
       
          <aura:set attribute="actions">
       
              <lightning:button label="Contact Data" onclick="{!c.Condata}" />
          </aura:set> 
   
    </lightning:card>
    <aura:iteration items="{!v.Contact}" var="con">
            <tbody>
                <tr>
                    <td>{! con.FirstName}</td>
                    <td>{! con.LastName}</td>
                    <td>{! con.Email}</td>
                </tr>
                     
            </tbody>
        </aura:iteration>
</aura:component>


In the controller.js of the above component

({
 Condata : function(component,event,helper) {
 helper.callServer(
            component,
            "c.getContacts",
            "v.Contact"
        );
      
    }
})



<aura:application extends="force:slds">

    <c:CallApexClassCmpBase/>
     
 </aura:application>

Thanks
Shivdeep​

All Answers

Shivdeep KumarShivdeep Kumar
Hi Smita,

You are passing 4 parameter in callServer helper method but in js controller you passed only 3.
Dont try to pass "function(response) { console.log('Contacts data'+JSON.stringify(response)); }" directly on helper method.

Thanks
smita bhargavasmita bhargava
Hi shivdeep
Could you let me know how to call the helper method in the js controller?

thanks

 
Shivdeep KumarShivdeep Kumar
Hi Smita,

Try below code and let me know if this help !
 
Base.cmp
--------
<aura:component abstract="true">
	{!v.body}
</aura:component>


BaseHelper.js
--------------
({
    callServer : function(component,method,attributeName,params) {
        var action = component.get(method);
        if (params) {
            action.setParams(params);
        }
       
        action.setCallback(this,function(response){
            var state = response.getState();
        	console.log('State log '+state);
            if(state === 'SUCCESS'){
                component.set(attributeName,response.getReturnValue());
            }else if(state === 'ERROR'){
		console.log('Error '+response.getError());
	}

        });
        $A.enqueueAction(action);
    }
})


ApexController.axpc
-------------------
public with sharing class ApexController {
    @AuraEnabled
    public static List<Contact> getContacts() {
        List<Contact> contacts = [select Id, Name, FirstName,LastName,MailingStreet, Phone, Email FROM Contact];
        return contacts;
    }
}


CallApexClassCmpBase.cmp
------------------------

<aura:component controller="ApexController" extends="c:Base">
	<aura:attribute name="Contact" type="List"/>
    <lightning:card title="Bring Contact data">
       
          <aura:set attribute="actions">
       
              <lightning:button label="Contact Data" onclick="{!c.Condata}" />
          </aura:set> 
   
    </lightning:card>
    <aura:iteration items="{!v.Contact}" var="con">
            <tbody>
                <tr>
                    <td>{! con.FirstName}</td>
                    <td>{! con.LastName}</td>
                    <td>{! con.Email}</td>
                </tr>
                     
            </tbody>
        </aura:iteration>
</aura:component>


In the controller.js of the above component

({
 Condata : function(component,event,helper) {
 helper.callServer(
            component,
            "c.getContacts",
            "v.Contact"
        );
      
    }
})



<aura:application extends="force:slds">

    <c:CallApexClassCmpBase/>
     
 </aura:application>

Thanks
Shivdeep​
This was selected as the best answer
smita bhargavasmita bhargava
Thanks that worked.