You need to sign in to do that
Don't have an account?
markdamo
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.
Turns out I had somehow created an empty class called User. Deleted this and everything working again.
Thanks for your help.
Mark.
All Answers
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?
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).
I even tried to create a new class to just assign a user but it failed as well with the same error.
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
Turns out I had somehow created an empty class called User. Deleted this and everything working again.
Thanks for your help.
Mark.
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!!!