1

Query 1 : SELECT id,name FROM apexclass where (status like 'active')

this query is returning valid result.

enter image description here

Query 2 : SELECT id,name FROM apexclass where (body like 'istest') is returning below error.

I can't see any difference between those two column, but if i execute query 2, i am getting below error:

[object Object]: id, name FROM apexclass where (body like 'istest') ^ ERROR at Row:1:Column:39 field 'body' can not be filtered in query call

This error usually comes when we query like this

SELECT id,name FROM apexclass where body like 'istest'
1
  • 3
    Body of Apex class is like long text area, and you can not filter long text area fields in SOQL
    – kurunve
    Commented Apr 6, 2017 at 9:39

3 Answers 3

5

To answer your question, Status is of type Picklist while Body is a Long Text Area. You can find out all you need to know about the ApexClass object in the SOAP API Developer Guide.

Filtering on Long Text Area fields is not supported. Take a read of Long Text Field Limitations if needed.

An alternative approach to your need would be to make used of the contains() String method. Something like:

List<ApexClass> acsWithBody = new List<ApexClass>();
for(ApexClass ac : [SELECT id, Name, Body FROM ApexClass]) {
    if ac.Body.contains('istest');
    acsWithBody.add(ac);
}
2
  • 3
    Alternatively, you can use FIND '@isTest' IN ALL FIELDS RETURNING ApexClass(Name WHERE Status='Active').
    – sfdcfox
    Commented Apr 6, 2017 at 13:14
  • I was trying to get all test classes of another environment using rest by this query SELECT id,name FROM apexclass where body like '@isTest' That's why i need only this query to execute. We cannot query SOSL by Rest. Commented Apr 7, 2017 at 5:54
0

You can not put like operator against body field.

However if you want to see what test classes are there you can check under 'Apex Test Execution' --> 'Select Tests'

e.g. http://media.screensteps.me/sherod/pye7gn/apex-test-execution-salesforcecom-unlimited-edition-1.png?1360808765

0

I was trying to get all test classes of another environment using rest by this query SELECT id,name FROM apexclass where body like '@isTest' That's why i need only this query to execute. We cannot query SOSL by Rest.

Now i created one services with this method to get All Test classes.

@HttpGet
global static List<TestClass> getTestClasses(){
  List<TestClass> TestClassList = new List<TestClass>();
   ApexClass[] tstClasses = [FIND '@isTest' IN ALL FIELDS RETURNING ApexClass(Id, Name ORDER BY Name)][0];            
    if(tstClasses != null){
        for(ApexClass cls : tstClasses){
          TestClass tc = new TestClass();
           tc.Id = cls.Id;
           tc.Name = cls.Name;
           TestClassList.add(tc);
           }
         }
   return TestClassList.size()>0?TestClassList:null;
}


global class TestClass{
        public String Id{get; set;}
        public String Name{get; set;}
    }

You must log in to answer this question.

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