0

I have a query that checks if a custom button is clicked by the record Owner or a Salesforce Admin if not throw an error message. My code is below:

List<Opportunity> opportunityList = new List<Opportunity>([select Id from Opportunity where ID=:System.currentPageReference().getParameters().get('Id') and OwnerId=:UserInfo.getUserId()]);

My current code check if the user is the owner of the record. Now I want to check if the user is also an Administrator. I want to check the ProfileId and check if it matches the Admin Profile Id.

Get the Admin ID: String adminId = [Select Id from Profile where Name='System Administrator'];
Add to Query: UserInfo.getProfileId() = :adminId does not seem to work when I do the following:

List<Opportunity> opportunityList = new List<Opportunity>([select Id from Opportunity where ID=:System.currentPageReference().getParameters().get('Id') and (OwnerId=:UserInfo.getUserId() or UserInfo.getProfileId()=:adminId)]);

I also tried :UserInfo.getProfileId()=:adminId does not seem to work

Any suggestions?

2 Answers 2

1

UserInfo is returning details about the current user. So if that's who you're looking to check, just compare it directly.

if(adminId != UserInfo.getProfileId()) {
   // error 
}

If you're checking the owner, you want to compare their profileId. Just add owner.profileId to the query.

if(adminId != opportunity.owner.profileId) {
  // error
}
0

When putting a condition statement in query it should be about some of the fields you quering otherwise it has no meaning. You can put (UserInfo.getProfileId()==adminId) in if condition outside of the query. And the apply needed logic. If you would post more code maybe i could help more.

You must log in to answer this question.

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