I'm just getting setup on my Mac, to work with my organization. I wanted to be able to open the Terminal and use sfdx to run SOQL queries but I think perhaps I missed some crucial step?

I was successful in running this command:

sfdx force source retrieve -m ApexClass

This seems to have downloaded everything from my organization, all of the Apex classes, all of the folders, the structure of everything.

So I thought I had everything setup. However, I seem to be unable to run SOQL queries. For instance if I do this:

sfdx force:data:soql -t -q "select count(Id), UserRole.Name  
from User 
where isactive = true 
group by UserRole.Name" -r csv > user_roles_results.csv

I get this error:

Error: No such relation 'UserRole' on entity 'User'. If 
you are attempting to use a custom field, be sure to 
append the '__c' after the custom field name. Please 
reference your WSDL or the describe call for the 
appropriate names.

And yet if I log into Salesforce and open a Developer Console then I can run the same query just fine:

select count(Id), UserRole.Name  
from User 
where isactive = true 
group by UserRole.Name

And that gives me the results I expect.

What do I need to do to get the same results when I run sfdx in my Terminal?

Am I missing some crucial meta data, or something?

  • 1
    You can get errors like this because the contextual user does not have necessary permissions to access the objects or fields. Check to see if you connect with the same account that you log in with.
    – Phil W
    Commented Mar 26, 2023 at 21:32
  • I set the default user to the username that I use to log in as an admin. Is there a command I can run that would let me see who I am logged in as via the CLI? Commented Mar 26, 2023 at 23:50
  • Are you using sfdx to login to the org as well as run CLI commands? You can use force:org:open IIRC.
    – Phil W
    Commented Mar 27, 2023 at 7:56
  • Careful - you can have active sessions in the same browser for multiple orgs. If you use the force:org:open CLI command you can see which org is going to be connected to with the same (default or explicit) alias/org.
    – Phil W
    Commented Mar 27, 2023 at 14:40
  • 1
    Okay, I ran "sfdx force:org:open" and it opened the browser to our production account, which is great, and it showed me using my username for the production account, which is great. So I think I'm correctly "auth"ed for production, right? So whatever the problem with the SOQL is something else? Commented Mar 27, 2023 at 15:07


You must log in to answer this question.

Browse other questions tagged .