I have this scenario where I have overridden Opportunity New button with a Visualforce page. I am trying to find a way where I need to redirect users to a Lightning App when the user is in Lightning Experience or else redirects to Visualforce page when the user is in Classic. Idea is to have compatibility on both world as we are planning to migrate users to Lightning in phased manner.

I have created Lightning Application for Opportunity. I also understood how to detect the user context based on UIThemeDisplayed. What I am not clear how to navigate user to Lightning app if a user is in Lightning experience.

    public static Boolean isSF1 {get {
        String theme = UserInfo.getUiThemeDisplayed();
        if(theme == 'Theme4t'){
            isSF1 = true;
            isSF1 = false;
        return isSF1;
} set;}

    public PageReference ContinueToNew() {
    PageReference opp = new PageReference('/apex/Opportunity_NewMode');
    if (isSF1) {
        ******* I would like to replace the following line of code to Lightning component******
        if (string.isBlank(AccountID)) {
            opp = new PageReference('/apex/SE_AccountSearchPage');
        } else {
            opp = new PageReference('/apex/SE_CompanyLookupSF1');

2 Answers 2


You can use following code
UserInfo.getUiThemeDisplayed() will identify logged in user instance, if its Lightning or classic, based on that you can form your URL to redirect user to your specific page.

String serverInstance = Utilities.getOrganizationDetails().InstanceName.toLowerCase();
    this.baseUrl = 'https://' + this.serverInstance + '.lightning.force.com/one/one.app#/n/';
    this.redirectUrl = this.baseUrl+Utilities.getNamespacePrefix()+'YourExactPageNameinLEX';
    this.baseUrl = 'https://' + this.serverInstance + '.salesforce.com';
    this.redirectUrl = this.baseUrl+'/apex/'+Utilities.getNamespacePrefix()+'YourExactPageNameinClassic';

Add this Utility Class to get your org name space and organization details,if your using package with namespace, this will append the required namespace in your URL.

public with sharing class Utilities{
* @description: Method To return NameSpace,
* @param: None
* @return: None.
public static String getNamespacePrefix() {
    Integer indexOfToken = -1;
    String namespace = '';
    for (Schema.SObjectType type : Schema.getGlobalDescribe().values()) {
        String sobjName = String.valueOf(type);
        if (null != sobjName) {
          indexOfToken = sobjName.indexOf('YourOrgCustomObject');
        if (indexOfToken != -1) {
            namespace = sobjName.substring(0, indexOfToken);
    return namespace;

* @description method to get the Organization Details
* @param None
* @return Organization
public static Organization getOrganizationDetails()
    if(orgDetails != null)
        return orgDetails;
        orgDetails = [SELECT
        limit 1];
        return orgDetails;

ap5 = Server Instance which will be returned by following code, which will vary with different orgs.

String serverInstance = Utilities.getOrganizationDetails().InstanceName.toLowerCase();

In Lightning:-
Final URL:- https://ap5.lightning.force.com/one/one.app#/n/NAMESPACE__YOURPAGE

In Classic:-
Final URL: https://NAMESPACE.ap5.visual.force.com/apex/Your_Classic_Page

  • Thanks for your response. I was able to redirect using "'/one/one.app#/n/LightninigOpp'" from the server side controller. So I am not sure why we need get to the serverInstance and Utilities controller for forming the url.
    – user28452
    Nov 21, 2017 at 11:09
  • I just gave you a generic code I am not aware of your actual implementation, so thought to give a generic one which will work in all orgs wherever you will deploy your code, my intent is to describe URL formation in two experience i.e Lightning and Classic. Ultimately its forming the same URL i.e /one/one.app#/n, cheers if your problem is already solved. Nov 21, 2017 at 11:17
  • serverInstance say e.g ap5 are different if you are operating in different orgs, also if some one has register domain then URL also changes according to domain name so you can't hardcode all these things, you need to have URL which will work according to respective org configuration setting, also if org has namespace prefix added then you need to have namespace added inside your URL for the pages you are referring to . Nov 21, 2017 at 11:21
  • /one/one.app#/n/YourPage this will fail if org has namespace added as "ABC", if namespace is there then URL should be like /one/one.app#/n/ABC_YourPage. Nov 21, 2017 at 11:27

If you want to replace above apex code with js then there is a method in js to do so

navigate : function (url){
      if(sforce && sforce.one) {
            else {
                window.location.href = '/apex/SE_AccountSearchPage';

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .