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
markdamomarkdamo 

Illegal assignment from LIST<User> to User

Today I am running into an Apex error ' Illegal assignment from LIST<User> to User' with the following piece of code.

 

User u =[Select u.Profile.Name, u.ProfileId From User u where u.UserName = :username];

 

This code worked ok last week with no errors but today I cannot get this to work and I dont know why!

 

Ive tried to change the code to 

User[] u =[Select u.Profile.Name, u.ProfileId From User u where u.UserName = :username];

but this also errors out with 'Illegal assignment from LIST<User> to LIST<User>'

 

Any help/pointers would be greatly appreciated.

 

Best Answer chosen by Admin (Salesforce Developers) 
markdamomarkdamo

Turns out I had somehow created an empty class called User.  Deleted this and everything working again.

 

Thanks for your help.

Mark.

All Answers

sfdcfoxsfdcfox

There's not enough detail here, and I've not been able to replicate it or even really know how that error could come about. I literally copy/pasted your code in my developer org and it runs (assuming I assign String Username). My best guess is that your code is running in with sharing context on some user that has limited access to view users (portal user, maybe?). Maybe you could give us some context about what you're trying to do, the surrounding code, and any other observations you might have?

markdamomarkdamo

Basically what I was trying to do is to redirect the user to a page on the Site I setup depending on their profile ID.  Below is the modified version of the SiteLoginController.cls that is no longer working.  This code worked on Friday, but now I cannot make any changes to it as it throws the above error.

 

I have noticed trying to select a user on some other classes MyProfilePageController (line 15).

 

/** * An apex page controller that exposes the site login functionality */ global class SiteLoginController { global String username {get; set;} global String password {get; set;} global String error {get; set;}
global PageReference login() { string sURL = '/'; User[] u =[Select u.Profile.Name, u.ProfileId From User u where u.UserName = :username]; if(u.size()>0){ if (u[0].Profile.Name == 'Authenticated Customer')sURL = '/support/dashboard'; } pageReference redirect = Site.login(username, password, sURL); if(redirect==null)error = 'Check your username and password. Invalid login.'; return redirect; } global SiteLoginController () {} global static testMethod void testSiteLoginController () { // Instantiate a new controller with all parameters in the page SiteLoginController controller = new SiteLoginController (); controller.username = 'test@salesforce.com'; controller.password = '123456'; System.assertEquals(controller.login(),null); } }

I even tried to create a new class to just assign a user but it failed as well with the same error.

 

public with sharing class TEST {
	
	public User getUser(){
		User u = [select id from User limit 1];
		return u;
	}
}

 

Error: Description Resource Path Location Type

Save error: Illegal assignment from LIST<User> to User TEST.cls /Merlin Interactive - Dev Site/src/classes line 4 Force.com save problem

 

 

markdamomarkdamo

Turns out I had somehow created an empty class called User.  Deleted this and everything working again.

 

Thanks for your help.

Mark.

This was selected as the best answer
DownstairsBDownstairsB
Wow MarkDamo1 this really saved me a lot of time.
I don't know how I created a User Class by accident... but I would have spent DAYS trying to track this one down.

thanks!!!