+ Start a Discussion
RadDude89RadDude89 

Validation based on current users custom field

Hi,

I'm trying to introduce validation on one of our apex classes which is based on a custom field value on the user's record.

So we have a custom field setup on the user object called Consultant (checkbox).
When a user logs in I want to return an error message if that current logged in user has Consultant = FALSE then show them the error message whereas for users where Consultant = TRUE they can bypass the error.

List<User> lstUser = [Select Agent_Name__c from User where userinfo.Consultant__c=TRUE];
 if(lstUser.size()==0 ){
                                apexpages.addmessage(new ApexPages.Message(ApexPages.severity.FATAL,' You can not proceed'));

When I try using userinfo. it doesn't compile so does anyone know how to retrieve a value from a custom field on user object for the current user?

Best Answer chosen by RadDude89
@anilbathula@@anilbathula@
Hi RadDude89,

Try this code snippet:-

String ids=userinfo.getUserId();
List<user>usr=[Select Agent_Name__c from User where Consultant__c=TRUE And ID=:ids];
if(lstUser.size()==0 ){
                                apexpages.addmessage(new ApexPages.Message(ApexPages.severity.FATAL,' You can not proceed'));

Thanks
Anil.B
 

All Answers

@anilbathula@@anilbathula@
Hi RadDude89,

Try this code snippet:-

String ids=userinfo.getUserId();
List<user>usr=[Select Agent_Name__c from User where Consultant__c=TRUE And ID=:ids];
if(lstUser.size()==0 ){
                                apexpages.addmessage(new ApexPages.Message(ApexPages.severity.FATAL,' You can not proceed'));

Thanks
Anil.B
 
This was selected as the best answer
RadDude89RadDude89
That works, thanks for your help