I am using partner api to get the list of classes in salesforce. But issue is that at a time we can only get 2000 classes, I tried using a while loop to pass the list of classes already queried but then I get exception for :

    QueryResult className = new QueryResult();
    String queryloop = "SELECT Id, Name FROM APEXCLASS WHERE Name NOT IN ('"+"')";
    while (true){
        com.sforce.soap.partner.sobject.SObject[] records = partnerConnection.query(queryloop).getRecords();
        String[] stringArray = new String[records.length];
        for (int i = 0; i < records.length; i++) {
            String name = (String) records[i].getField("Name");
            stringArray[i] = name;

        String join = "'" + StringUtils.join(stringArray,"','") + "'";

        queryloop = "SELECT Id, Name FROM APEXCLASS WHERE Name NOT IN ("+join+")";

        if(records.length == 0){

Exception :

[MalformedQueryFault [ApiQueryFault [ApiFault  
 exceptionMessage='SOQL statements can not be longer than 20000 characters.'

How can I get all the classes in my org? Is there any other way to retrieve the classes in my org. My org has more than 4000 classes and I have to fire a query.

1 Answer 1


You will need to change your strategy as including all the class names you have already queried is causing the query string to exceed the 20,000 character query limit.

The Partner API includes the queryMore() mechanism (that you follow the initial query() call with) that allows you to start one query and then get the results of that one query in batches with no duplicates. This avoids the need to build the NOT IN that is causing the query string length limit to be hit.


You must log in to answer this question.

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