0

I am new to Apex and Lightning so what I am doing may not be the correct way to accomplish this task. I am building a custom lightning component (CreateNewMailDrop.cmp) with a button. The button click needs to run some Javascript in my lightning controller (CreateNewMailDropController.js). I want to run getCount() in the .js file. I then need the js to run an Apex method (getCountRequest()) from the class MailDropETFClientManager and return a string to the javascript. If I am doing something wrong, I suspect it is either my function call in the javascript (component.get("c.getCountRequest")) or in how my Apex class is defined/imported as the aura "controller=...", but I don't know what to do next. I am open to suggestions of performing this action differently if it makes more sense.

CreateNewMailDropController.js

({
    init : function(component, event, helper) {
        var today = $A.localizationService.formatDate(new Date(), "YYYY-MM-DD");
        component.set('v.today', today);
        component.set('v.original_date_of_contract_min_value', '1990-01-01')
        component.set('v.original_date_of_contract_max_value', today)
    },
    getCount : function(component, event, helper) {
        var action = component.get("c.getCountRequest");
        action.setCallback(this, function(response) {
           if (response.state === "SUCCESS"){
               var serverResponse = response.getReturnValue();
               alert("API Action Success");
           }
           else if (response.state === "INCOMPLETE")
           {
                alert("API Action Incomplete");
           }
           else if (response.state === "ERROR") {
                var errors = response.getError();
                if (errors) {
                    if (errors[0] && errors[0].message) {
                        alert("Error message: " + 
                                errors[0].message);
                    }
                } else {
                    alert("Unknown error");
                }
            }
            else{
                alert("Response State:" + String(response.state));
            }       
        });
        $A.enqueueAction(action);
    },
...

CreateNewMailDrop.cmp

<aura:component implements="force:lightningQuickAction,flexipage:availableForAllPageTypes" access="public" controller="MailDropETFClientManager">
    <aura:attribute name="maincmp" type="Aura.component[]"/>
    
    <aura:handler name="init" value="{!this}" action="{!c.loadOptions}" />
    <aura:handler name="init" action="{!c.init}" value="{!this}" />

...

<lightning:button onclick="{! c.getCount }">Check Counts</lightning:button>
    <lightning:button onclick="{! c.getNewMailDrop }">Create New Mail Drop</lightning:button>

</aura:component>

MailDropETFClientManager.cls

@AuraEnabled
    public static String getCountRequest()
    {
    
        String job = 'test'; //create new custom API Job object and load id here
        
        ...
        
        return job;
    }

1
  • A couple of comments: why develop aura components instead of LWC (which are easier to understand and develop and are far more performant) and how come you have two handlers called "init"?
    – Phil W
    Jul 17 at 6:11
0
<aura:handler name="init" value="{!this}" action="{!c.loadOptions}" />
<aura:handler name="init" action="{!c.init}" value="{!this}" />

You're trying to call two methods, loadOptions and init, neither of which are in your JavaScript controller (CreateNewMailDropController.js). You need to call the getCount method if you want to call it.

<aura:handler name="init" action="{!c.getCount}" value="{!this}" />

Where init is the name of the event to handle, which is fired when a component initializes, action is the method you want to call, which must be a JavaScript controller method, and value is a special value that tells the method which instance of the component is being initialized (a component may appear more than once in a page).

As Phil commented, it's unusual to have more than one init handler, but not forbidden. Just make sure your action is calling the method you intend to call. Also, as a final note, there's a limitation on how Aura works. You absolutely must not name any Apex method in the class the same as any JavaScript controller method, or the call will fail. You are also not allowed to define the same method with the same name more than once within either Apex or JavaScript.

2
  • Hi sfdxcfox, Thank you for the quick answer. I was intending for the lightning button click to run the getCount function. The other js functions are not shown since my file is rather large. The function runs, but the callback I configured returns a " SecureAction: [object Object]{ key: {"namespace":"c"} }" instead of string. The response.state value is undefined. I think I am missing something else Jul 19 at 18:37
  • @All, I was able to discover by switching my Apex method's return type from String to List<String>, the function returns as expected. No errors. Apex methods return Json serialized data, but I don't exactly understand why a simple string couldn't be serialized. Either way, problem solved Jul 21 at 17:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.