0

I am working on a task to retrieve test classes filtered on the basis of package that they are part of. I am trying to run the below query to do so but gets error when trying to filter on the basis of InstalledPackage.

sfdx force:data:soql:query --query="SELECT Id,Name,SymbolTable FROM ApexClass WHERE InstalledPackage = 'XYZ'" --usetoolingapi --targetusername=myorg --json | jq '.result.records[] | select(.SymbolTable != null) | select(.SymbolTable.methods[].annotations[].name == "IsTest")' | jq '.Name' | uniq

Error:

"status": 1,
  "name": "INVALID_FIELD",
  "message": "\nSymbolTable FROM ApexClass WHERE InstalledPackage = ''\n
             ^\nERROR at Row:1:Column:49\nNo such column 'InstalledPackage' on entity 'ApexClass'. 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.",

image of Salesforce UI

Could you please let me know if I need to update the query or there is any other way to retrieve test classes filtered based on package.

Thanks.

7
  • 2
    Hi, and welcome to SFSE! Please take a moment to read about How to Ask and take the tour. Also, just in case, please read about the X-Y Problem, which may be relevant here. You can't query based on the Installed Package field, which is a type of "fake field," but depending on your "X" (from above), there might be another way to get the information you need. You can edit your question to include more information.
    – sfdcfox
    Aug 2, 2021 at 5:06
  • Done, I believe I've now added all the information that maybe required.
    – Ashwani
    Aug 2, 2021 at 5:43
  • Where are you getting your schema from for ApexClass? I don't see InstalledPackage listed anywhere as a field you can query. Or are you guessing that it's the field you're after?
    – Nick C
    Aug 2, 2021 at 7:38
  • @NickCook I found the similar query and trying to use after adding it a WHERE clause. Is their any issue in syntax? or else is their any other way to fulfill the requirement?
    – Ashwani
    Aug 2, 2021 at 7:53
  • @Ashwani But where did you find the 'similar query'? is it in official docs? If not, how do you know if it works for whoever wrote it?
    – Nick C
    Aug 2, 2021 at 8:05

1 Answer 1

2

It's hard to tell exactly what you're trying to do, but the following might work:

SELECT Id, Name, NamespacePrefix FROM ApexClass WHERE NamespacePrefix = 'XYZ'

To find out what fields you can query on any object, you can use the Developer Console.

e.g.

enter image description here

Where you can clearly see the fields you can add to your query, rather than just guessing based on the UI.

enter image description here If not, please be clearer in your question about what you're after.

6
  • This was the similar query that I found and after looking at the Salesforce UI, I added 'InstalledPackage' field. I am new to Salesforce not much aware with the internals of Salesforce. Maybe the query is not working but at least from the initial lines, you must have got that I am trying to retrieve test apex classes of a particular package.
    – Ashwani
    Aug 2, 2021 at 8:16
  • @Ashwani It is possible, you can go to Installed Packages, find the necessary package, copy its namespace prefix and run query: SELECT Id, Name, NamespacePrefix FROM ApexClass WHERE NamespacePrefix = 'package_namespace_prefix' Aug 2, 2021 at 11:49
  • @MariiaIllarionova it will work if apply filter on NamespacePrefix but we need to apply filter on package name. What happens if we have 2 package with same namespace?
    – Ashwani
    Aug 3, 2021 at 3:12
  • @NickCook Thanks for the analysis and showing fields that can be queried. Now, could you suggest if their is any other way to do the task?
    – Ashwani
    Aug 3, 2021 at 4:35
  • @Ashwani You do not need any other approach. For 1 namespace there os only 1 Managed Package. If the Apex class is from Unmanaged Package, Installed Package in Apex Classes List View will be empty. Aug 3, 2021 at 5:09

You must log in to answer this question.

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