3

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?

1 Answer 1

5

Whenever I've called executeAnonymous it has been on either the Apex WSDL or the Tooling WSDL. As far as I know it isn't included in either the Enterprise or Partner WSDL.

When you call the Apex WSDL you need to change the ServerURL that of the Apex Service rather than the Enterprise Server.

It will be something like:

https://cs9.salesforce.com/services/Soap/s/28.0/00D9YourOrgId

The important part here is the /s/ rather than /c/ in the URL to indicate which web service you are calling.

A typical sequence will be:

  • Call the login method with a username and password (plus append the security token if required)
  • Get the ServerURL and SessionId from the LoginResult
  • Set the SessionId in the proxy class so it is sent with subsequent requests.
  • If you called login in the Enterprise service and then want to call the Apex service you will need to alter the ServerURL (replace /c with /s). The Apex proxy class endpoint needs to be set to this modified ServerURL.
  • Call executeAnonymous.
1
  • thanks a ton, I added : String loginUrl = config.getServiceEndpoint().replace("/Soap/c/", "/Soap/s/"); and it works great!
    – Joelio
    Aug 9, 2013 at 12:14

You must log in to answer this question.

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