You need to sign in to do that
Don't have an account?
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.
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.
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
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,
--Akram
Error: Compile Error: Incompatible types since an instance of List<SObject> is never an instance of List<User> at line 4 column 22
--Akram
Below code is working fine for me. Please check below screen shot. Try to debug your code
If still above code will not work then please try below code
Please let us know if this will help you
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!
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
<apex:pageBlockTable value="{! Users }" var="u">
Have a nice weekend