Has anyone figured out a way to differentiate between Lightning Desktop and Salesforce1?

There are mechanisms to detect if a user is using Lightning Desktop / Salesforce1 vs Salesforce Classic (Aloha). See here for a Javascript solution.

I know officially (same link - see 'Limitations of Detecting sforce.one') there is no supported mechanism yet. I've also read about the Visualforce workaround that is out at the moment.

I want to know specifically a front end way to tell the two interfaces apart.

Thanks Nelson

  • 3
    does user-agent help here by any chance ? – Vamsi Krishna Oct 13 '15 at 14:02
  • Thanks I'm looking into it. I think there's a problem with certain devices that don't send a standard 'token' in the user agent so that we can easily tell what device it is. One I can think of is the Kindle Fire HDX. Here's a good resource pointing out common User Agent strings for Tablets and Mobile : developer.mozilla.org/en-US/docs/… – Nelson Ramalho Oct 13 '15 at 15:22
up vote 9 down vote accepted

With Spring 16 release, we are getting new values added to the existing $User.UITheme Global variable and to the UserInfo.getUiTheme() method which will help us to identify the current user's UI mode.

here's the list of possible values

  • Theme1 — Obsolete Salesforce theme
  • Theme2 — Salesforce Classic 2005 user interface theme
  • Theme3 — Salesforce Classic 2010 user interface theme
  • Theme4d — Modern “Lightning Experience” Salesforce theme
  • Theme4t — Salesforce1 mobile Salesforce theme
  • PortalDefault — Salesforce Customer Portal theme
  • Webstore — Salesforce AppExchange theme

hopefully we can use this in Workflows, Validations, Formulas, Apex & Visualforce..

Official Reference

  • Many Thanks Vamsi. I'll select this as the correct answer as there is an API opened up to developers now. – Nelson Ramalho Feb 9 '16 at 13:57
  • 1
    It is worth being aware that there is a known issue with Salesforce Classic in mobile. The Theme displayed shows as Theme4t rather than Theme3, as would be expected: success.salesforce.com/issues_view?id=a1p3A000000jl1UQAQ – Bigears Sep 22 '16 at 14:48

Until there's an official Salesforce way I'm going with a JS solution to detect if the user is using a mobile device. I'm using JS to find the user agent and match against the keyword 'Mobi':

var isMobile = function() {
     //using regex here so developers can add more keywords if needed
     return navigator.userAgent.match('(Mobi)') ? true : false;
}

If the user is using a browser on a mobile device, they will be navigated to Salesforce1. Also the User Agent for the Salesforce1 App (Android and iOS) has 'SalesforceMobileSDK'.

Thanks Vamsi Krishna for pointing me in the right direction.

  • 1
    glad u got it working.. does it cover tablets as well ?? – Vamsi Krishna Oct 16 '15 at 1:58
  • If the tablet has Mobi in the user agent then yes. You could add 'Tablet' to the regex : '(Mobi|Tablet)' . I found Mobi on its own worked well. Phone's I tested with: Android, iPhone and Windoze. Tablets : Android, iPad and Surface. Everything seemed in sync with what Salesforce is doing (The Surface navigated to Lightning experience and isMobile returned false). – Nelson Ramalho Oct 16 '15 at 10:15
 public static string isLightningExperience()
 {
        boolean isLightningExperience = false;
        string id = UserInfo.getUserId();
        for (User u: [SELECT Id, UserPreferencesLightningExperiencePreferred FROM User WHERE Id =: id LIMIT 1])
        {
            isLightningExperience = u.UserPreferencesLightningExperiencePreferred;
        }
        return string.valueOf(isLightningExperience);
 }

the value of the field UserPreferencesLightningExperiencePreferred in the object user change when the user interface switch. if UserPreferencesLightningExperiencePreferred is ture => Lightning Desktop , if false => Classic Desktop

  • 1
    Just putting code will not help. Please explain how this resolves the issue. – Reshma Mar 1 '17 at 10:19
  • the value of the field UserPreferencesLightningExperiencePreferred in the object user change when the user interface switch. if UserPreferencesLightningExperiencePreferred is ture => Lightning Desktop , if false => Classic Desktop – Khaled Youssef Mar 27 '17 at 10:22
  • Please use edit to update your answer and not put it as a comment – Reshma Mar 27 '17 at 10:27

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.