Newer Version Available

This content describes an older version of this product. View Latest

Use Tooling API with SOAP

Use SOAP API if you’re using a strongly typed language like Java that generates Web service client code. Tooling SOAP API is used just like the Salesforce SOAP API. For details on usage, syntax, and authentication, see the SOAP API Developer's Guide.

To access the Tooling API WSDL, from Setup, click Develop | API and click Generate Tooling WSDL.

Like the Salesforce SOAP API, Tooling API uses the following calls.

Call Description
create() Adds one or more records to your organization’s data.
delete() Deletes one or more records from your organization’s data.
describeGlobal() Lists the available Tooling API objects and their metadata.
describeSObjects() Describes metadata (field list and object properties) for the specified object or array of objects.

Call describeGlobal() to retrieve a list of all Tooling API objects for your organization, then iterate through the list and use describeSObjects() to obtain metadata about individual objects.

executeanonymous(string apexcode) Executes the specified block of Apex anonymously and returns the result.
query() Executes a query against a Tooling API object and returns data that matches the specified criteria.
queryMore() Retrieves the next batch of objects from a query().
retrieve() Retrieves one or more records based on the specified IDs.
runTests() and runTestsAsynchronous() Executes test methods in the specified classes. Running tests asynchronously allows methods to process in parallel, cutting down your test run times. For example code, see ApexTestQueueItem.
update() Updates one or more existing records in your organization’s data.
upsert() Creates records and updates existing records; uses a custom field to determine the presence of existing records.

Starting in API version 34.0, the following calls support the AllOrNoneHeader header: create(), update(), upsert(), and delete().

By default, if AllOrNoneHeader isn’t used, those calls can save a partial set of records for records with no errors (equivalent to AllOrNoneHeader=false). If AllOrNoneHeader is set to true, records are saved only when there are no failures in any record in the call.

Examples

These examples use Java, but you can use Tooling SOAP API in any language that supports Web services.

To compile Apex classes or triggers in Developer Edition or sandbox organizations, use create(). The next sample uses ApexClass to compile a simple class with a single method called SayHello.

The IsCheckOnly parameter on ContainerAsyncRequest indicates whether an asynchronous request compiles code but doesn’t execute or save it (true), or compiles and save the code (false).

The next example expands upon the first by modifying the SayHello() method to accept a person’s first and last name. This example uses MetadataContainer with ApexClassMember to retrieve and update the class, and ContainerAsyncRequest to compile and deploy the changes to the server. You can use the same method with ApexTriggerMember, ApexComponentMember, and ApexPageMember.
To test your code, modify the IsCheckOnly parameter in the next sample, and log in to your organization after a successful execution to verify the results.
  • When IsCheckOnly = true, the SayHello() method remains the same. ApexClassMember contains the compiled results, but the class on the server remains the same.
  • When IsCheckOnly = false, the SayHello() method shows the change to accept a person’s first and last name.

Note

To access Apex class and trigger data in a structured format, use a SymbolTable.

The next sample queries the ApexClassMember object created in the previous example to obtain the SymbolTable of the modified class.

The SOQL statement used depends on when the data is retrieved.

  • To execute the query from within the previous sample, use the ID of the ContainerAsyncRequest. For example, SELECT Body, ContentEntityId, SymbolTable FROM ApexClassMember where MetadataContainerId = '" + requestId + "'"
  • Otherwise, use the ID of the modified class as shown in the next sample. For example, SELECT ContentEntityId, SymbolTable FROM ApexClassMember where ContentEntityId = '" + classId + "'"

Note

To add checkpoints to your code for debugging, use ApexExecutionOverlayAction.

This sample adds a checkpoint to the class from the previous samples: