Apex – Navigate to Records in Lightning and Classic!

As I have mentioned in previous posts, I am working on an org using Salesforce Classic but I have been building any new UI features using Lightning Components and embedding them in Visualforce page so they will work the same in both Classic and Lightning.

Recently I came across an issue where I needed to navigate to a record after it was inserted but I found an issue in that it is handled different in Salesforce1 app and Lightning compared to Classic.

I was able to resolve the issue by using the code below in the (Javascript)Controller of the Lightning Component. This means that user was directed to new record in every scenario!

[sourcecode type=”plaintext”]

// SF1 & Lightning Navigation
if( (typeof sforce != ‘undefined’) && (sforce != null) ) {
sforce.one.navigateToSObject(accId, ‘detail’);
}
// Desktop Navigation
else {
window.location.href = "/"+accId;
}

[/sourcecode]

Leave a Reply