1

is there a way to detect if I stay in Salesforce Classic or Salesforce Lightning context from javascript as static resource?

I need this because before to detect the user theme I used

if(sforce && sforce.console && sforce.console.isInConsole())

but for some months now it doesn't work more because sforce can be used also in Lightning.

Do you know another trick to detect theme from JS?

1

In short, no, a static resource script cannot, by itself, reliably determine the current theme setting. You would need cooperation from some sort of outside source. For example, you might write an Apex Class to be called as a web service, import the AJAX toolkit, and call sforce.apex.execute, or call a Visualforce remote method, or write a Visualforce page that you call out to in order to get the value of {!$User.UIThemeDisplayed}. All four methods will require that you set up at least one class or Visualforce page to get the desired effect.

1

I found the solution:

if(UITheme.getUITheme() === 'Theme3')

The line above does the magic from a Javascript static resource! It is added in Summer '18, see here.

  • I used the function directly in my javascript file of lightning web component but it gave me 'UITheme' is not defined. error. What should I do differently? – Koustubh Masurkar Nov 5 at 6:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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