function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SF OperatorSF Operator 

Unable to set UserPreferencesLightningExperiencePreferred

I'm ussing a trigger to set that:

trigger SetUserPreferencesLightning on User (before insert, before update) {
  string url = Apexpages.currentPage().getUrl();
    for(User user : {
        user.UserPreferencesLightningExperiencePreferred = true;

This works for my admin profile but is not working for another users. (those users are not switched to lightning as default page but they can switch to lightning)

AnudeepAnudeep (Salesforce Developers) 
I would recommend the following instead of if(url.contains('lightning'))
for(User user : {
			System.debug('Debug Log for User Preference'+user.UserPreferencesLightningExperiencePreferred);
				if(user.UserPreferencesLightningExperiencePreferred = false){
                user.UserPreferencesLightningExperiencePreferred = true;

SF OperatorSF Operator
Thanks for your fast reply, I tried that way and the problem is when they switch back to classic and then switch to lightning, the UserPreferencesLightningExperiencePreferred stays at false.