I'm running the following SOQL query:

SELECT Profile.Name, Title FROM User WHERE Id = '<some id>'

Most of the times it works fine, but for some users I get the following error (I guess it's because the token I'm using doesn't have the permissions for the Profile object)

No such relation 'Profile' on entity 'User'

Is there a way to craft the query to return Profile.Name only when Profile is supported, and to return NULL (or similar) when we have no permissions to Profile?

  • I am expecting such queries to be made by Admins, in which case it shouldn't be a problem of permissions. :) – Mahmood Dec 3 '14 at 6:41
  • @MahmoodButt why? I'm running this query using the token of the user I'm requesting the information for. – Tzach Dec 3 '14 at 6:46
  • Well in that case you do have a problem. You cant achieve this task with just SOQL then. – Mahmood Dec 3 '14 at 7:08

You can fix this permissions error by changing your query to be dependent upon the current user's permissions:

if (Schema.sObjectType.Profile.isAccessible()) {
   u = [SELECT Profile.Name, Title FROM User WHERE Id = '<some id>'];
} else {
   u = [SELECT Title FROM User WHERE Id = '<some id>'];

This is some basic code to get you moving. You could use the same if logic and write the query dynamically.


You mentioned that the call is being made using the REST API. You could take the code I have written above and create an ApexREST method that you call instead of making your Query via the REST API. This is still RESTful, uses only a slightly modified endpoint and for yourself ensures that the code calling the service need not know this information in advance.

| improve this answer | |
  • Thanks. Unfortunately my code runs outside of SF and is using the REST API to run the query. I know I can solve it with two separate SOQL queries, however I wonder if this is doable with only one query. – Tzach Dec 3 '14 at 9:26
  • Just edited the answer - make the code i wrote an ApexREST endpoint and you are good to go. – pbattisson Dec 3 '14 at 9:36

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.