[SalesForce] Error when calling Apex Controller methods from Lightning Component Helper: [Cannot read property ‘get’ of undefined]

End goal – have a doneRendering event which calls an Apex method to determine if a scheduled job exists or not.

I have an apex controller method that I've verified works in execute anonymous in the dev console. I've verified that every line up until this one in my helper method works:

        var action = cmp.get("c.isSchedulerActivated");

I've triple checked syntax and everything lines up with other controller methods fired from click events, so the only thing I can think is that doneRendering might be firing the event before I can target the apex controller method?

Component:

<aura:component implements="force:appHostable" controller="updateUsingLightningCheckboxController">
<aura:handler event="aura:doneRendering" action="{!c.doneRendering}"/>
<aura:attribute name="isDoneRendering" type="Boolean" default="false"/>
<aura:attribute name="isAppActive" type="Boolean" default="false"/>
</aura:component>

Controller:

({

doneRendering: function(cmp, evt, helper) {
    if(cmp.get("v.isDoneRendering") === false){
        cmp.set("v.isDoneRendering", true);
        console.log("Is done rendering set to true? "+ cmp.get("v.isDoneRendering"));
        helper.isActivated();
        console.log("isActivated is done"); //this does not fire
    }//if
} })

Helper:

({
isActivated: function(cmp) {
    console.log("Helper method entered");
    var action = cmp.get("c.isSchedulerActivated"); //THIS IS THE LINE THAT RESULTS IN ERROR THROWN

    action.setCallback(this, function(response) {
        var state = response.getState();
        console.log("result of isSchedulerActivated is:  "+ response.getReturnValue());
        if (state === "SUCCESS") {
            console.log("state === success  ");
        }//if
    });
    $A.enqueueAction(action);
} })

Apex Controller:

public with sharing class updateUsingLightningCheckboxController {

@AuraEnabled
public static Boolean isSchedulerActivated(){
    Boolean isActivated=FALSE;
    CronTrigger[] cts= [SELECT Id, NextFireTime, State FROM CronTrigger WHERE CronExpression =: updateUsingLightningCheckboxUtils.cronJobSchedule];
    if (cts.size()>0)
        isActivated=TRUE;
    return isActivated;
}//isSchedulerActivated }//updateUsingLightningCheckboxController

I'm struggling to figure this out, and this is the last thing I need to resolve before publishing an app that creates a Lightning Adoption Dashboard so any help is greatly appreciated!!

Best Answer

I would use init event instead of done rendering event and use setTimeout() to continuously poll to check the scheduled status .

Also try passing the cmp to the helper

helper.isActivated(cmp)

Here is an example of how to poll http://www.jitendrazaa.com/blog/salesforce/calling-apex-method-at-regular-interval-from-lightning-component/#more-5715

Related Topic