1

I'm trying to figure out why navigateToSObject is coming back undefined. I'm new to Lightning so I'm sure it is something simple I'm missing.

The debug showed $A returning SecureAura: [object Object]{ key: {"namespace":"c"} }

I'm calling the method from the component here:

<button class="slds-button slds-button--brand" onclick="{!c.gotoContact}">Go To Contact</button>

}, 
gotoContact: function(cmp, event, helper) {     
    var action = cmp.get('c.getContact'); 
    action.setParams({ guid : cmp.get("v.guid") });
    action.setCallback(this, function(response) {
        var state = response.getState();
        if(state == "SUCCESS") {
            var sobjectEvent = $A.get("e.force:navigateToSObject");
            sObjectEvent.setParams({
                "recordId": response.getReturnValue(),
                "slideDevName": "detail"
            });
            sObjectEvent.fire();                    
        }
    });
    $A.enqueueAction(action);       
},
9
  • Please include an exact copy of the error message text you are getting. – Keith C Apr 30 '18 at 20:21
  • @Keith C error is ReferenceError: Error in $A.getCallback() [sObjectEvent is not defined] Callback failed: apex://SSOViewOverrideController/ACTION$getContact – Dman100 Apr 30 '18 at 20:25
  • 1
    You seem to be using sObjectEvent outside the scope where it is defined. You have defined it within the if block, whereas you are using it outside that block. – Jayant Das Apr 30 '18 at 20:33
  • 2
    And this time, it's case sensitive. You need to change var sobjectEvent to var sObjectEvent (notice the upper O). – Jayant Das Apr 30 '18 at 20:57
  • 1
    @JayantDas great catch, JS is case sensitive.so variable names should be proper. – Pranay Jaiswal Apr 30 '18 at 20:59
3

Rolling up comment as answer:

As per your original post, you had declared the event variable within the if block and you were using it outside the scope and thus you were getting undefined variable error.

if(state == "SUCCESS") {
        var sobjectEvent = $A.get("e.force:navigateToSObject"); // declared here
        sObjectEvent.setParams({
            "recordId": response.getReturnValue(),
            "slideDevName": "detail"
        });

    }
});
$A.enqueueAction(action);     
sObjectEvent.fire();  // used here and thus error

Later when you moved it to the if block, it was case sensitivity which was not working.

if(state == "SUCCESS") {
        var sobjectEvent = $A.get("e.force:navigateToSObject");
        sObjectEvent.setParams({
            "recordId": response.getReturnValue(),
            "slideDevName": "detail"
        });
        sObjectEvent.fire(); // it was case sensitivity this time, declaration needed to be changed to sObjectEvent (notice the upper O)                   
    }
});
$A.enqueueAction(action);   

Finally, changing the variable name to sObjectEvent and using it within the block where declared resolved the issue.

3
  • I would also add, "don't use long variable names where they are not needed"; less chance of typo problems using e.g. var e = $A.get(.... – Keith C Apr 30 '18 at 21:13
  • I would say it's more dependent on individual's style. I kind of like to use more relevant names for variables. I would have possibly used navEvt here which resembles it's a navigation event. – Jayant Das May 1 '18 at 3:45
  • My point is that long names in case-sensitive and non-compiled JavaScript are prone to typos whereas short names are not. In short fragments of code the context - what the e represents - is clear and doesn't need to be expressed in a long variable name. But agree its a matter of company/individual style. – Keith C May 1 '18 at 8:15

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.