Detect if Visualforce Page is in Salesforce1

I have a Visualforce page that is used in both Salesforce1 and the desktop. On that page is a button. That button should only display if the user is loading the page on the desktop. It should be hidden on Salesforce1. Any suggestions?

Essentially I am just looking for a way to detect if the Visualforce page is loaded in Salesforce1 or not. I can handle the logic of displaying the button, I just don’t know if it is possible to tell if it is Salesforce1 or not.

Answer

In the Salesforce Spring 16 release, there will be global variables and apex methods you can call to detect the user’s UI theme context. This may make it easier for developers to detect the user’s experience without relying on the existence of certain javascript objects or URL parameters.

http://docs.releasenotes.salesforce.com/en-us/spring16/release-notes/rn_vf_uitheme.htm#rn_vf_uitheme

The User.UITheme and User.UIThemeDisplayed global variables and the UserInfo.getUiTheme() and UserInfo.getUiThemeDisplayed() Apex utility methods are improved to support the Salesforce1 mobile app and Lightning Experience.

These existing variables and system calls have expanded the range of return values to support the new user experience contexts. Possible return values include the following.

  • 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

These global variables and system calls can replace other, more fragile methods of detecting the user experience context, such as testing for the presence of the sforce.one JavaScript global. Use them to write Visualforce pages and apps that adapt to the user experience context in which they’re running.

Attribution
Source : Link , Question Author : Jesse Altman , Answer Author : Doug Ayers

Leave a Comment