+ Start a Discussion
Yemi Roti OlanbiwonnuYemi Roti Olanbiwonnu 

getUITheme not working (returns Theme3 in LEX and Classic)

I am having an issue with detecting lightning in our Production environment.

We have a redirect VF page that is overriding the edit action of one of our custom objects. The controller for this VF page does a check for lightning by checking UserInfo.getUiThemeDisplayed(). However, for some reason, the org is only returning Theme3 at the moment. I tested in a dev console, and typing in
System.debug( 'UI Theme displayed: ' + UserInfo.getUiThemeDisplayed() );
results in Theme3, regardless of whether I am running that anonymous code in Classic or LEX. I assume this is due to a weird quirk with the developer console. But the VF page is giving me the same response.

I am aware of a current bug with the UserInfo.getUiTheme[Displayed] functions that cannot detect the actual page the User is viewing. It only looks at the setting. For example, if the user clicks the link to switch back to Classic, but then opens a link that is specifically for lightning (eg hostname.my.salesforce.com/lightning/r/Account/[id]/view), it will open that link in the LEX context. But getUiTheme and getUiThemeDisplayed will still think that the page is in Classic since the User's setting is set to Classic. However, that bug is not what is happening here for me.

In my current situation, even if I click the "switch to lightning experience" link, it still continues to show Theme3 for getUiThemeDisplayed. I know we can detect lightning by checking the URL, but I'd rather avoid that hacky and unstable workaround.

Has anyone else experienced this kind of behavior and could give me some insights as to what is happening?

Somthing else that may be important to know is that I am currently unable to reproduce this in my dev org or any of our Sandboxes at the moment.
Best Answer chosen by Yemi Roti Olanbiwonnu
GovindarajGovindaraj
Hi Yemi,

Did u try below global variables,

$User.UITheme – Returns the theme that is supposed to be used.
$User.UIThemeDisplayed – Returns the theme that is actually being used.

As, Detecting the UI context via a server call is expensive, 

Thanks,
Govindaraj.S

All Answers

GovindarajGovindaraj
Hi Yemi,

Did u try below global variables,

$User.UITheme – Returns the theme that is supposed to be used.
$User.UIThemeDisplayed – Returns the theme that is actually being used.

As, Detecting the UI context via a server call is expensive, 

Thanks,
Govindaraj.S
This was selected as the best answer
Yemi Roti OlanbiwonnuYemi Roti Olanbiwonnu
Hi Govindaraj,

I'm not sure what you mean by "detecting the UI context via a server call is expensive". Either ways that I try, it will be through the server. I was using UserInfo.getUiThemeDisplayed() in the constructor, so that information is retrieved while the page is still being prepared on the server. If I use {!$User.UIThemeDisplayed} on the VF page, the actual value is calculated on the server and it will replace {!$User.UIThemeDisplayed} with the actual theme on the returned html page. In both scenarios, the actual theme is pulled from the server before being sent to the client. Either ways, I'm geting off topic.

I tried using the value in the UI and it turns out htat it was Theme4u (since the dev console showed Theme3, I assumed that was the value in the VF page because the controller was checking for UserInfo.getUiThemeDisplayed() == 'Theme4d'. But it turns out that it was Theme4u. It looks like there was a CRUC from Salesforce in Spring 18 that caused this behavior (https://releasenotes.docs.salesforce.com/en-us/spring18/release-notes/rn_general_lightning_console_theme_cruc.htm). Apparently, the page was in a lightning console app, so that is why it was not being detected. I will work on checking for Theme4u as well. Thanks!

Regards,
Yemi Olanbiwonnu
nakul deokar 7nakul deokar 7
Great Work