6

I was able to get the names of all the apex classes in an org using metadata api with Java

public void getApexClasses() throws ConnectionException {
    ListMetadataQuery[] queries = new ListMetadataQuery[1];

    ListMetadataQuery queryApexClass = new ListMetadataQuery();
    queryApexClass.setType("ApexClass");

    queries[0] = queryApexClass;

    FileProperties[] fileProperties = mConnection.listMetadata(queries, 29.0);
    for(FileProperties fileProperty : fileProperties) {
        System.out.println(fileProperty.getType() + ":" + fileProperty.getFullName());
    }
}

Now, I would like to get the content of each of these apex classes. How can I do that?

ApexClass apexClass = new ApexClass();
// Some code to load the apexClass instance - HELP required
System.out.println(apexClass.getContent());

1 Answer 1

7

You have a few options for this...

  • Metadata API, once Spring'14 is rolled out (this month) you can use the new readMetadata operation on the Metadata API to retrieve the ApexClass.
  • Partner API and SOQL, you can currently use SOQL to actually query (via the query operation) the Body field on this object, e.g.

    Select Id, Name, Body From ApexClass
    
  • Tooling API and SOQL, this also allows you to query as above with the Partner API

Note: These later two options leverage the fact that the ApexClass object is an standard object and thus queryable via SOQL. The Body field will be null if the class resides in a managed package.

You must log in to answer this question.

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