1

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,"','") + "'";
        className.setRecords(records);

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

        if(records.length == 0){
            break;
        }
    }

Exception :

[MalformedQueryFault [ApiQueryFault [ApiFault  
exceptionCode='MALFORMED_QUERY'
 exceptionMessage='SOQL statements can not be longer than 20000 characters.'
 extendedErrorDetails='{[0]}'
]
row='-1'
column='-1'
]

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.

5

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.

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.