Execute anonymous Apex using SOAP API

Posted on 31-01-2013 09:43 by graham
This tutorial shows how to execute Apex code using the executeAnonymous method from Salesforce Apex API.

Generate Web Service client


First, you need to generate a Web Service client from the WSDL files provided by Salesforce. In this case, download the Apex API WSDL and build a client as described in this wiki page http://wiki.developerforce.com/page/Introduction_to_the_Force.com_Web_Services_Connector

Connect to Salesforce


Before executing any Apex code, you need to connect and authenticate to Salesforce. The following code does it:
public static String login (String username, String password)
{
// this can be changed to "http://login.salesforce.com" if you are using a production sandbox
String loginUrl = "http://test.salesforce.com";

final ConnectorConfig config = new ConnectorConfig();
config.setAuthEndpoint(loginUrl);
config.setServiceEndpoint(loginUrl);
config.setManualLogin(true);
LoginResult result = (new PartnerConnection(config)).login(username, password);

// return the session ID
return loginResult.getSessionId();
}

Executing Apex


Now that we are authenticated to Salesforce, we can make the actual call to method SoapConnection.executeAnonymous().
public static ConnectorConfig getConnectorConfig (String username, String password)
{
// this can be changed to "http://login.salesforce.com" if you are using a production sandbox
String loginUrl = "http://test.salesforce.com";

final ConnectorConfig config = new ConnectorConfig();
config.setAuthEndpoint(loginUrl);
config.setServiceEndpoint(loginUrl);
config.setManualLogin(true);
LoginResult result = (new PartnerConnection(config)).login(username, password);

// now we need to create a new connection to the server URL returned by the login response
if (result.isSuccess())
{
ConnectorConfig config = new ConnectorConfig();

// do NOT use the "test.salesforce.com" URL here - it won't work
config.setAuthEndpoint(result.getServerUrl());
config.setServiceEndpoint(result.getServerUrl());
config.setSessionId(loginResult.getSessionId());
}
else
{
return null;
}
}

public static void executeApex (String apexCode)
{
SoapConnection conn = new SoapConnection(getConnectorConfig("username", "password"));
ExecuteAnonymousResult result = conn.executeAnonymous(apexCode);
}

public static void main(String [] args)
{
executeApex("System.debug('Hello')");
}

Note: While calling setAuthEndpoint and setServiceEndpoint, be sure to use the serverUrl returned by the log in call, not the default log in endpoint test.salesforce.com or login.salesforce.com

Obtaining debug logs


If you look at the ExecuteAnonymousResult object returned by the call, you may be suprised (I was!) to discover that it does not contain any debugging info for successful calls. If you want to get debug logs for your call, you need to set an appropriate debugging header on your Salesforce connection. This can be done by calling:
SoapConnection conn = new SoapConnection(config);
conn.setDebuggingHeader (...);

The setDebuggingHeader method allows you to specify what parts of code you want to debug and how detailed your logs should be. In our example we will set all the debugging information to the finest level, so that we can get as much data as possible:
SoapConnection conn = new SoapConnection(config);

LogInfo infoAll = new LogInfo();
infoAll.setCategory(LogCategory.All);
infoAll.setLevel(LogCategoryLevel.Finest);

LogInfo infoApex = new LogInfo();
infoApex.setCategory(LogCategory.Apex_code);
infoApex.setLevel(LogCategoryLevel.Finest);

LogInfo infoProfiling = new LogInfo();
infoProfiling.setCategory(LogCategory.Apex_profiling);
infoProfiling.setLevel(LogCategoryLevel.Finest);

LogInfo infoDB = new LogInfo();
infoDB.setCategory(LogCategory.Db);
infoDB.setLevel(LogCategoryLevel.Finest);

conn.setDebuggingHeader(new LogInfo[] { infoAll, infoApex, infoProfiling, infoDB }, LogType.Detail);

After executing the call, you can access your debug logs like this:
conn.getDebuggingInfo().getDebugLog();
Comments
I'm having trouble getting this to work. I can get everything to run, but conn.getDebuggingInfo() always returns null, and I have no idea why. Any idea why this program wouldn't be working?

https://gist.github.com/dradtke/7602458
Added on 22-11-2013 17:29 by anonymous

 

Add comment

Has this tutorial been helpful to you? Or do you see anything wrong? We appreciate your opinion!
Your comment:
Show formatting hints
HTML is disallowed, but in your text you can use the following markup
  • [code][/code] for a block of code
  • [tt][/tt] for inline code
  • [link]link href|link anchor[/link] for links
  • [b][/b] for bold text
Email:
+ Ask a question
If you have a technical question related to programming and computers, ask it here. Other users will help you solve it!
Unanswered questions
Share your knowledge by helping others solve their problems