I'm trying to assign a role to a user using Apex. Here my code:

user.UserRole=[select UserRoleId from UserRole where Name='CEO'].UserRoleId;

But the debugger give the error:

illegal assignment fro Id to UserRole.

1 Answer 1


You're trying to assign UserRole to UserRoleId - trying to assign sObject to Id.

Correct code would be:

user.UserRoleId=[select Id from UserRole where Name = 'CEO'].Id;

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .