[SalesForce] Error in lightning component

Can you, please, explain for me, why I have got this error:

Uncaught Error in $A.getCallback() [TypeError: Cannot read property
'apply' of undefined] throws at
https://margaritabarabash-dev-ed.lightning.force.com/auraFW/javascript/hiaIVQRy4mbq1QTz7P5TfA/aura_proddebug.js:14722:9

part of my component markup:

<aura:component controller="FieldHistoryTrackingComponentController" implements="flexipage:availableForAllPageTypes,force:appHostable,forceCommunity:availableForAllPageTypes">

    <ltng:require styles="/resource/SLDS203/assets/styles/salesforce-lightning-design-system.css" />

    <aura:registerEvent name="appEventRefreshView" type="force:refreshView" />
    <aura:registerEvent name="DisplayAllHistoryEvent" type="c:DisplayAllHistoryEvent"/>

    <aura:handler name="init" action="{!c.doInit}" value="{!this}"/>
    <aura:handler event="force:refreshView" action="{!c.onReloadView}" />
    <aura:handler event="c:DisplayAllHistoryEvent" action="{!c.handleDisplayAllHistoryEvent}"/>
</aura:component>

My js-controller:

({
doInit : function(component, event, helper) {
    if(component.get("v.id")== null){
        var id;
        var url = window.location.href;
        if(url.indexOf("id=") == -1){
            var splitURL = url.split('/');
            id = splitURL[splitURL.length - 2];
            if(id.length == 18){
                id = id.substring(0, 14);
            }
        } else {
            id = url.substring(url.indexOf("id=") + 3, url.indexOf("id=") + 18);
        }
        component.set("v.id", id);
    }
    component.set("v.AllRecords", false);

    var action = component.get("c.getHistoryRecords");
    action.setParams({
        "sObjectId": component.get("v.id"),
        "countOfRecords": component.get("v.countOfRecords")
    });

    action.setCallback(this, function(response) {
        var state = response.getState();
        if (component.isValid() && state === "SUCCESS") {
            component.set("v.HistoryChanges", response.getReturnValue());
        }
        else {
            console.log("Failed with state: " + state);
        }
    });

    $A.enqueueAction(action);
},

select : function(component, event, helper) {
    if(event.target.tagName === 'A'){
        var countOfRecords = event.target.firstChild.firstChild.nodeValue;
    } else{
        var countOfRecords = event.target.firstChild.nodeValue;
    }
    component.set("v.countOfRecords", countOfRecords);
},

goToAllHistoryRecords: function(component, event, helper) {
    var urlEvent = $A.get("e.force:navigateToURL");
    urlEvent.setParams({
        "url": "/one/one.app#eyJjb21wb25lbnREZWYiOiJvbmU6ZmxleGlwYWdlIiwiYXR0cmlidXRlcyI6eyJ2YWx1ZXMiOnsiZmxleGlQYWdlRGV2ZWxvcGVyTmFtZSI6IkFsbF9GaWVsZF9IaXN0b3J5X1JlY29yZCIsImxhYmVsIjoiQWxsIEZpZWxkIEhpc3RvcnkgUmVjb3JkIiwiaWNvblVybCI6Imh0dHBzOi8vbWFyZ2FyaXRhYmFyYWJhc2gtZGV2LWVkLm15LnNhbGVzZm9yY2UuY29tL2ltZy9pY29uL3Q0djM1L2N1c3RvbS9jdXN0b20yMV8xMjAucG5nIiwiaWNvbkNvbG9yIjoiOGE3YWVkIn19LCJ0IjoxNDczNDE0NzIzMjc2fQ%3D%3D"      
    });
    urlEvent.fire();

    var DisplayAllHistoryEvent = component.getEvent("DisplayAllHistoryEvent");

    DisplayAllHistoryEvent.setParams({
        id = component.get("v.id"),
        countOfRecord = 1000,
        BackUrl = window.location.href
    }).fire();
},

back: function(component, event, helper) {

},

handleDisplayAllHistoryEvent : function(component, event, helper) {
    component.set("v.id", event.getParam("id"));
    component.set("v.countOfRecord", event.getParam("countOfRecord"));
    component.set("v.BackUrl", event.getParam("UrlFrom"));
    component.set("v.AllRecords", true);
},

goToUserRecord: function (component, event, helper) {
    var url = window.location.href;
    var domain = 'https://' + url.split('/')[2].split(':')[0] + '/';
    var redirect = domain + event.target.getAttribute("id");
    component.set("v.linkToUser", redirect);
},

onReloadView: function (component, event, helper) {
    var action = component.get("c.getHistoryRecords");
    action.setParams({
        "sObjectId": component.get("{!v.id}"),
        "countOfRecords": component.get("{!v.countOfRecords}")
    });

    action.setCallback(this, function(response) {
        var state = response.getState();
        if (component.isValid() && state === "SUCCESS") {
            component.set("v.HistoryChanges", response.getReturnValue());
        }
        else {
            console.log("Failed with state: " + state);
        }
    });

    $A.enqueueAction(action);
}

})

Thanks very much for answer. I have not a clue what`s wrong with this.

Best Answer

I think it's this line that needs to be fixed:

if(id.lenght == 18){
    id = id.substring(0, 14);
}

Change lenght to length.

Generally the apply errors mean there is a javascript error - and that's what you have. Of course, there may be more, but this is currently the only one I can see.

Oh and make sure you don't try to write to window.location. Looks like reading from it is ok though.

Related Topic