+ Start a Discussion
Taresh PandeyTaresh Pandey 

How to check user page whether it is opened from salesforce classic or Lightning App UI. ?

Hello All,
     I'm stuck in the middle of this problem I don't know how check from which source user page extrated. If I open user page first in salesforce classic and after I switch it to Lightning.
Thanks to all of you in advance

Regards'
Taresh Pandey
Best Answer chosen by Taresh Pandey
Vladimir GergievVladimir Gergiev
Unfortuantely no, because you can check current interface (classic or lightning) only from client-side. So there is no way to do this in pure Apex, but you can perform call to apex code from these javascript code.

All Answers

Vladimir GergievVladimir Gergiev
Hi Taresh,
You can check this with simple Javascript code on your page:
function isLightningExperienceOrSalesforce1() {
    return((typeof sforce != 'undefined') && sforce && (!!sforce.one));
}

and the use this function in next way:
if( isLightningExperienceOrSalesforce1() ) {
    // Do something for Lightning Experience
}
else {
    // Use classic Visualforce
}
You can find more infromation in related Trailhead module: Sharing Visualforce Pages Between Classic and Lightning Experience (https://developer.salesforce.com/trailhead/lex_dev_visualforce/lex_dev_visualforce_multipurpose_pages)
Taresh PandeyTaresh Pandey
Thank you very much for replying.
Vladimir Gergiev
This code might help me but I need pure apex hand for the remote calling. Do you have any other solution for the issue; in Apex. 
Vladimir GergievVladimir Gergiev
Unfortuantely no, because you can check current interface (classic or lightning) only from client-side. So there is no way to do this in pure Apex, but you can perform call to apex code from these javascript code.
This was selected as the best answer
Taresh PandeyTaresh Pandey
Some where I knew this. And your statement cleared my Doubt completely. thanks  
Admin User 3309Admin User 3309
Try following  helper method to determine if page is executed from mobile or desktop, this also works for lightning UI. 
 public static Boolean isSalesForce1orLE() {
    if(ApexPages.currentPage().getParameters().get('sfdcIFrameHost') != null ||
       ApexPages.currentPage().getParameters().get('sfdcIFrameOrigin') != null ||
       ApexPages.currentPage().getParameters().get('isdtp') == 'p1') {      
        return true;
    }
    else {      
        return false;
    }           
}
Yachana YachanaYachana Yachana
Hi, You can also try

if((typeof sforce != 'undefined') && sforce && (!!sforce.one))
              sforce.one.navigateToSObject(account.Id);
           else
               window.location = "/"+account.Id;
this code is chceking if you are in lightning experience then going through container one app else simply doing window.location.
 
VenkateshVenkatesh
Thanx Yachana , your answer helped me.
Anas LABRITIAnas LABRITI
From client side you can do it by checking the url  : 
    
        if(document.referrer.indexOf(".lightning.force.com") > 0){
            alert("welcome to lightning ");
        }else{
            alert("welcome to classic");
        }
 
Miriam GutiérrezMiriam Gutiérrez
Hi Everybody!!

I want to return to the record page from where the flow was called (in the same window/tab, we don't want a new window to be opened). 

My VF page looks like this:
<apex:page standardController="MyCustomObject" tabStyle="MyCustomObject__c" extensions="MyCustomObjectExt">
    	<flow:interview name="MyFlow" finishLocation="{!callfinishFlow}">
        <apex:param name="recordId" value="{!myfield__c.Id}"/>
    </flow:interview>
    
    <script type="text/javascript">
    	function finishFlow(){
            if((typeof sforce != 'undefined') && sforce && (!!sforce.one))
            sforce.one.navigateToSObject(myfield__c.Id);
            else
                window.location = "/"+myfield__c.Id;

         }  
	</script>
</apex:page>

I've also tried using sforce.one.navigateToURL("/"+myfield__c.Id); and  window.location.assign = "/"+{!myfield__c.Id}; with no success.

As I'm already using a standard controller, I can't use a controller to redirect to the record page. So, I'm using an extension which calls to my javascript code. If we are in SF1 then, sforce.one should fire. If we are in classic then, window.location should fire.

The extension code is:
 
public class MyCustomObjectExt{         

    public string callfinishFlow{get;set;}         

     public MyCustomObjectExt(ApexPages.StandardController myStdController)    
     {      
              callfinishFlow='<script> finishFlow(); </script>';     
     } 
}

But I get this invalid redirection message:

Invalid Page Redirection
The page you attempted to access has been blocked due to a redirection to an outside website or an improperly coded link or button. Please contact your salesforce.com Administrator for assistance. For more information, see Insufficient Privileges Errors. 

Click here to return to the previous page.



Thanks for your help!
Miriam​
Tomasz SobotaTomasz Sobota

You can use this sample code:

String uiThemeDisplayed = UserInfo.getUiThemeDisplayed()


It returns theme (Classic/Lightning) or it is displayed as mobile app. See in documentation:

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_userinfo.htm

Alexis KasperaviciusAlexis Kasperavicius
You can also do this if you need to detect within a Flow. For example, create a boolean formula called isLightning with the following
 
CASE({!$User.UIThemeDisplayed}, "Theme4d",  1, "Theme4t", 1, "Theme4u", 1, "PortalDefault", 1, "Webstore", 1, 0) = 1