2

My developer is off for the week and I need to change a wee bit of code. I do not know much Apex.

Current code to make Admin exempt

//System Admin is exempt, 00e80000000k3t2AAA id of System Admin Profile
    if(UserInfo.getProfileId() == '00e80000000k3t2AAA'){
        System.debug('Admin exempt from Invoice & Billing Period validation.');
        return true;
    }

All I want to do is just change the Admin ID to a custom profile ID so users can continue their work.

When I change the ID to

//System Admin is exempt, 00e80000000k3t2AAA id of System Admin Profile
    if(UserInfo.getProfileId() == '00e80000001SQTxAAA'){
        System.debug('Admin exempt from Invoice & Billing Period validation.');
        return true;
    }

It will not let the user under that profile be exempt.

I have tried id 00e80000001SQTxAAA and 00e80000001SQTx.

Any ideas of how to get the custom profile id to work?

3
  • 3
    you should not be using a hard coded profile Id as this will fail if you move it to another environment as any ids are specifice to each instance. – Dave Humm Jan 2 '18 at 14:46
  • This is the same ID in the sandbox as in the live org. They were copied over from the live org. Any other ideas? – ADJ Jan 2 '18 at 14:56
  • 1
    I wouldn't do this- wait for your developer to come back. – battery.cord Jan 2 '18 at 14:58
3

You need to explicitly run a SOQL query to retrieve profile name and use that in the code as follows:

String profileName = [Select Id,Name from Profile where Id=:Userinfo.getProfileId()].Name;

if(profileName == 'System Administrator'){
    System.debug('Admin exempt from Invoice & Billing Period validation.');
    return true;
}

This will eliminate the Id dependency for different orgs.

1
  • Thank you for the solution! I understand this is not best practices with the IDs and I will get with my developer when she comes back. I appreciate you helping me out in the meantime so my users aren't in a bind for month end and year end financials. I need to get better at this Apex coding thing ;) – ADJ Jan 2 '18 at 15:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.