Hi I am trying to write a simple command line java program to execute anonymous apex.
I found a couple of examples that dont quite compile and am trying to put them together.
Here is what I have done so far:
- generated my enterprise and apex wsdl from my sandbox
generated my enterprise.jar and apex.jar using these directions - http://wiki.developerforce.com/page/Introduction_to_the_Force.com_Web_Services_Connector
wrote the java program here:
import com.sforce.soap.apex.Connector; import com.sforce.soap.apex.ExecuteAnonymousResult; import com.sforce.soap.apex.SoapConnection; import com.sforce.soap.enterprise.EnterpriseConnection; import com.sforce.ws.ConnectionException; import com.sforce.ws.ConnectorConfig;
public class Migrate {
public static void executeApex (String apexCode) throws ConnectionException { String USERNAME = "myuser"; String PASSWORD = "mypass+mytoken"; ConnectorConfig config = new ConnectorConfig(); config.setUsername(USERNAME); config.setPassword(PASSWORD); EnterpriseConnection enterpriseConnection = com.sforce.soap.enterprise.Connector.newConnection(config); System.out.println("Auth EndPoint: "+config.getAuthEndpoint()); System.out.println("Service EndPoint: "+config.getServiceEndpoint()); System.out.println("Username: "+config.getUsername()); System.out.println("SessionId: "+config.getSessionId()); ConnectorConfig soapConfig = new ConnectorConfig(); String loginUrl = "http://test.salesforce.com"; soapConfig.setAuthEndpoint(loginUrl); soapConfig.setServiceEndpoint(loginUrl); soapConfig.setManualLogin(true); soapConfig.setAuthEndpoint(config.getAuthEndpoint()); soapConfig.setServiceEndpoint(config.getServiceEndpoint()); soapConfig.setSessionId(config.getSessionId()); SoapConnection connection = new SoapConnection(soapConfig); ExecuteAnonymousResult result = connection.executeAnonymous(apexCode); } public static void main(String [] args) { try { executeApex("System.debug('Hello')"); } catch (ConnectionException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
}
When I run it I get:
Auth EndPoint: https://test.salesforce.com/services/Soap/c/28.0
Service EndPoint: https://cs9.salesforce.com/services/Soap/c/28.0/anumbernotsureishouldshare
Username: myusername
SessionId: (a long session id)
com.sforce.ws.SoapFaultException: No operation available for request {http://soap.sforce.com/2006/08/apex}executeAnonymous
at com.sforce.ws.transport.SoapConnection.createException(SoapConnection.java:204)
at com.sforce.ws.transport.SoapConnection.receive(SoapConnection.java:148)
at com.sforce.ws.transport.SoapConnection.send(SoapConnection.java:110)
at com.sforce.soap.apex.SoapConnection.executeAnonymous(SoapConnection.java:332)
at Migrate.executeApex(Migrate.java:39)
at Migrate.main(Migrate.java:45)
Any ideas what I am doing wrong?