[SalesForce] Uncaught Error in $A.getCallback() [Cannot read property ‘apply’ of undefined]

Here is My cmp

<aura:component controller="BlacklistController" 
                implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes" access="global" >

   <aura:attribute name="recordId" type="Id" />
    <aura:handler name="init" value="{!this}" action="{!c.doInit}" />

    <ui:outputText aura:id="Blacklist" value="This record is currently locked" class="Invisible" />    

</aura:component>

Here is my .js

({
    doInit : function(component, event, helper) {
        var action = component.get("c.CheckBlackListStatus");
        action.setParams({
            leadId: component.get("v.recordId")
        });

        action.setCallback(this, function(a) {
            var Blacklisted  

            If (Blacklisted == True){
                var MakeBlacklist = component.find("Blacklist");
                $A.util.removeClass(MakeBlacklist, 'Invisible');
                $A.util.addClass(MakeBlacklist, 'blue');
            }

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

Here is my Apex Controller

public with sharing class BlacklistController {
   String caseId {get; set;}

    @AuraEnabled
    public static Boolean CheckBlackListStatus(Id leadId) 
    {
        Lead lead = [SELECT Name, Status FROM Lead WHERE Id = :leadId];
        return lead.Status == 'Blacklisted';
            }
}

And my css

.THIS.blue {
    background-color: #16325C;
   display: block;
    margin-left: auto;
    margin-right: auto 
}

.THIS .logo
{
    display: block;
    margin-left: auto;
    margin-right: auto 
}
.THIS.Invisible {
    display: none;
}

Best Answer

You have a number of problems with your code, mostly related to the fact that, unlike Apex Code, JavaScript and Lightning are particularly case sensitive. Let's go over a few issues.

force:hasRecordId

When you use this interface, do not include a recordId attribute. Your component should look like this:

<aura:component controller="BlacklistController" 
                implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes" access="global" >
    <aura:handler name="init" value="{!this}" action="{!c.doInit}" />
    <ui:outputText aura:id="Blacklist" value="This record is currently locked" class="Invisible" />    
</aura:component>

JavaScript

It is case sensitive, so values like If and True are undefined. Also, you're not actually checking the return value from your callback. Here's how your client controller should be fixed:

({
    doInit : function(component, event, helper) {
        var action = component.get("c.CheckBlackListStatus");
        action.setParams({
            leadId: component.get("v.recordId")
        });

        action.setCallback(this, function(a) {
            var Blacklisted = a.getReturnValue();
            if(Blacklisted) {
                var MakeBlacklist = component.find("Blacklist");
                $A.util.removeClass(MakeBlacklist, 'Invisible');
                $A.util.addClass(MakeBlacklist, 'blue');
            }
        });
        $A.enqueueAction(action);
    }
})

Note: I'm not sure if just fixing those problems will fix your code's behavior, but it should get you closer to your goal.

Related Topic