7

I have a custom profile called MyCustomProfile. When I want to get profiles in my Apex class I don't get System Administrator. But in Developer Console, with same code, I can get both of them.

Profile[] adminProfiles = [SELECT id FROM Profile WHERE name IN ('MyCustomProfile', 'System Administrator')];

returns one element from my Apex class and returns two records from Developer Console. Why can't I see the System Administrator in my class, also? Do you have any ideas?

EDIT: Code part I'm getting the issue.

User userObj = [SELECT ProfileId FROM User WHERE id=:UserInfo.getUserId()];
Profile[] adminProfiles = [SELECT id FROM Profile WHERE name IN ('Merkez Kullanıcıları', 'System Administrator')];
Map<id, Profile> lIDToProfileMap = new Map<id, Profile>(adminProfiles);
system.debug('returned map: ' + lIDToProfileMap);
  • Does the described situation happen for the same user? In a comment below you mentioned that a user who runs the code is System Administrator. I understand that you tried it by yourself in Developer Console but did you also try to run this code in apex in system? – neff Sep 15 '14 at 21:08
  • Yes neff, I was actually meant exactly that on the comment you mentioned. I logged in as proper user and checked Debug Logs for that user. – mustafasahin Sep 16 '14 at 6:29
2

Ok, so if it doesn't work for a user (when you test as him) and works for you in a console I would say this is a problem of standard profile - System Administrator. One drawback of standard profiles is that they are translated to match the Language settings of current user. So if you're querying for Profile with name System Administrator as you do:

Profile[] adminProfiles = [SELECT id FROM Profile WHERE name IN ('Merkez Kullanıcıları', 'System Administrator')];

it will work for users with English set but will not work for German for instance. Custom system administrator profiles work because they are not translated. Based on 2nd profile name that you provided I guess that's an issue in this case (non english profile name). I guess the user has non English language and you have English set. Correct?

So if that's the issue what would be a solution? I'd suggest to:

  • hardcode this particular profile Id in a Constants class (if this is a Live system and profile Id will not change)

Profile[] adminProfiles = [SELECT Id FROM Profile WHERE Name = 'Merkez Kullanıcıları' OR Id = :MyConstantsClass.SYSTEM_ADMINISTRATOR_PROFILE_ID];

  • or create custom label storing profile name and add translations to it for each language that you're using

Profile[] adminProfiles = [SELECT Id FROM Profile WHERE Name IN ('Merkez Kullanıcıları', :System.Label.System_Administrator)];

You can see some other ideas on how to solve this over here:

  • Thank you neff, it's definitely the language! It won't come to my mind, obviously. Our user uses the system with Turkish translation. – mustafasahin Sep 16 '14 at 8:00
6

If your class is written With Sharing and if executed with normal user profile then you may see difference in result.

Note that : If not declared explicitly, classes are always WithOUT Sharing unless called from a method in another class defined as with sharing (http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_keywords_sharing.htm).

  • Yes Atul, class is written With Sharing. But the user who runs the code is System Administrator. Yet again, I'm getting this different results. – mustafasahin Sep 15 '14 at 12:31
  • Can you please share minimized version of your code so that we can re-produce this in our dev org? or even you can try that in other org and see if it makes any difference. My guess is that its permission issue. – AtulRajguru9 Sep 16 '14 at 6:26
  • I've shared the lines on post Atul. By the way, there is no permission set on system. – mustafasahin Sep 16 '14 at 6:36
  • I can see what you have shared already. The only line you shared will not help. And when i say permission issue it does not mean permission set. Permission issue can be caused due to profile or other security settings. We can help you only after looking at complete code. – AtulRajguru9 Sep 16 '14 at 6:58

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.