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;
}else{
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');
}
*****************************************************************************
}
}
Best Answer
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.
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.
ap5 = Server Instance which will be returned by following code, which will vary with different orgs.
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