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
Aron Schor [Dev]Aron Schor [Dev] 

Issue with Class: Error: Compile Error: Illegal assignment from List<SObject> to List<User> at line 4 column 1

I am having problems creating this Apex class, a little confused as I have done them before.

Error: Compile Error: Illegal assignment from List<SObject> to List<User> at line 4 column 1

public class LastLogin {
    private String sortOrder = 'LastLoginDate';
public List<User> getUsers() {
List<User> results = Database.query(
        'SELECT FirstName, LastName, Username, MobilePhone, IsActive, LastLoginDate FROM User ' +
        'ORDER BY ' + sortOrder + ' DESC'
        );
    return results;
}
}

Thanks.
Best Answer chosen by Aron Schor [Dev]
SarfarajSarfaraj

Hi Aron,

Can you please verify if you have any apex class named 'User' in your org. It is legal to define classes using names of standard objects but you should not. If you find one please delete it. Then you will see that both of the codes posted by me will compile and work.

--Akram

All Answers

SarfarajSarfaraj
Aron,

You have to cast the query result to List<User>. Database.query is a generic method that returns generic type and cannot be assigned without an explicit cast. Use this,
public class LastLogin {
    private String sortOrder = 'LastLoginDate';
public List<User> getUsers() {
List<User> results = (List<User>) Database.query(
        'SELECT FirstName, LastName, Username, MobilePhone, IsActive, LastLoginDate FROM User ' +
        'ORDER BY ' + sortOrder + ' DESC'
        );
    return results;
}
}

--Akram
Aron Schor [Dev]Aron Schor [Dev]
Thanks Akram, but it says

Error: Compile Error: Incompatible types since an instance of List<SObject> is never an instance of List<User> at line 4 column 22
 
SarfarajSarfaraj
Sorry. My bad, use this instead.
public class LastLogin {
    private String sortOrder = 'LastLoginDate';
public List<User> getUsers() {
List<sobject> results = Database.query(
        'SELECT FirstName, LastName, Username, MobilePhone, IsActive, LastLoginDate FROM User ' +
        'ORDER BY ' + sortOrder + ' DESC'
        );
List<User> userlist = new List<User>();
For(sobject o : results)
Userlist.add((user)o);
    return userlist;
}
}

--Akram
Aron Schor [Dev]Aron Schor [Dev]
Now it says: Error: Compile Error: Incompatible types since an instance of SObject is never an instance of User at line 10 column 14
Amit Chaudhary 8Amit Chaudhary 8
Hi Aron,

Below code is working fine for me. Please check below screen shot. Try to debug your code

User-added image
public class LastLogin 
{
    private String sortOrder = 'LastLoginDate';
    public List<User> getUsers() 
    {
			List<sobject> results = Database.query(
				'SELECT FirstName, LastName, Username, MobilePhone, IsActive, LastLoginDate FROM User ' +
				'ORDER BY ' + sortOrder + ' DESC'
			);
		
			List<User> userlist = new List<User>();
			For(sobject o : results)
				Userlist.add((user)o);
			
			return userlist;
	}
}

If still above code will not work then please try below code 
public class LastLogin 
{
    public List<User> getUsers() 
    {
			List<User> results = [ SELECT id , FirstName, LastName, Username, MobilePhone, IsActive, LastLoginDate FROM User ORDER BY LastLoginDate DESC ] ;
			return results;
	}
}

Please let us know if this will help you

 
SarfarajSarfaraj
Even the code that I posted initially works for me. Can you please post your full code alongwith the error message?
Aron Schor [Dev]Aron Schor [Dev]

Odd, I am still getting errors.  Do I have some odd setting or something else?

---> Error: Compile Error: Incompatible types since an instance of SObject is never an instance of User at line 13 column 30

public class LastLogin 
{
    private String sortOrder = 'LastLoginDate';
    public List<User> getUsers() 
    {
            List<sobject> results = Database.query(
                'SELECT FirstName, LastName, Username, MobilePhone, IsActive, LastLoginDate FROM User ' +
                'ORDER BY ' + sortOrder + ' DESC'
            );
        
            List<User> userlist = new List<User>();
            For(sobject o : results)
                Userlist.add((user)o);
            
            return userlist;
    }
}


______

---> Error: Compile Error: Illegal assignment from List<User> to List<User> at line 5 column 13

public class LastLogin 
{
    public List<User> getUsers() 
    {
            List<User> results = [ SELECT id , FirstName, LastName, Username, MobilePhone, IsActive, LastLoginDate FROM User ORDER BY LastLoginDate DESC ] ;
            return results;
    }
}

______

----->Error: Compile Error: Incompatible types since an instance of SObject is never an instance of User at line 10 column 14    


public class LastLogin {
    private String sortOrder = 'LastLoginDate';
public List<User> getUsers() {
List<sobject> results = Database.query(
        'SELECT FirstName, LastName, Username, MobilePhone, IsActive, LastLoginDate FROM User ' +
        'ORDER BY ' + sortOrder + ' DESC'
        );
List<User> userlist = new List<User>();
For(sobject o : results)
Userlist.add((user)o);
    return userlist;
}
}

 

Thanks guys!

SarfarajSarfaraj

Hi Aron,

Can you please verify if you have any apex class named 'User' in your org. It is legal to define classes using names of standard objects but you should not. If you find one please delete it. Then you will see that both of the codes posted by me will compile and work.

--Akram
This was selected as the best answer
Aron Schor [Dev]Aron Schor [Dev]
Ah, you are right.  What happened I think was I got an error and I clicked a link and it generated a class with that name.  I almost asked another question but I realized on the page I had to change to Users for it to work!  Thank's Akram.

            <apex:pageBlockTable value="{! Users }" var="u">
SarfarajSarfaraj
You are welcome. It was a tough one. It took us a lot of time to solve. 
Have a nice weekend