You need to sign in to do that
Don't have an account?

Populate field with userid when user equals certain role
Hi all,
Hopefully this is a simple one but I have a visualforce form which users use to load a lead, however, I want to populate a lookup field with the userid only when the user comes under a certain role.
I have tried this query to create the string
String userbasedid = [select ID from user where (userRoleid = '00EE0000000gXTj' AND id = :UserInfo.getUserId()].id;
Piece of Form Code:
IdentifyUser = userbasedid
So the form works fine when the users role equals the above userroleid, however, as soon as a different user with a different user role uses it I get the error:
List has no rows for assignment to SObject.
So I need to figure a way that when the query result is null it doesn't try to populate the identifyuser field.
Thanks
Kev
Hi Kev,
Here you have not done the Exception handling. just write your code in try block and do nothing in the catch block.
try{
String userbasedid = [select ID from user where (userRoleid = '00EE0000000gXTj' AND id = :UserInfo.getUserId()].id;
}
catch(Exception e){
}
this solves your problem.
Regards,
Shravan
Hi,
Thank you for your advice but I tried this once before and have just tried it again and for some reason I don't get the error but instead the userid doesn't get passed to the field.
Here's the code
Kev
Hi,
First of all you can invoke SOQL query directly on User and check the size of the records.
You can use getter and setter property on assign it with the id of user on the basis of condition.
List<User> usr = [select ID from user where (userRoleid = '00EE0000000gXTj' AND id = :UserInfo.getUserId()];
if(usr.size() >0){// your business logic here...}
Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.
Thanks, however, I tried the below and it didn't work:
List<User> usr = [select ID from user where (userRoleid = '00EE0000000gXTj' AND id = :UserInfo.getUserId()];
if(usr.size() >0)
{
userbasedid = usr.id;
}
I want to feed the above into the form here: