Hello Account (Java without wsc)

Java で Hello Account。

$ java -classpath ./:/usr/local/axis-1_4/lib/axis-ant.jar:/usr/local/axis-1_4/lib/commons-discovery-0.2.jar:/usr/local/axis-1_4/lib/jaxrpc.jar:/usr/local/axis-1_4/lib/wsdl4j-1.5.1.jar:/usr/local/axis-1_4/lib/axis.jar:/usr/local/axis-1_4/lib/commons-logging-1.0.4.jar:/usr/local/axis-1_4/lib/log4j-1.2.8.jar:/usr/local/axis-1_4/lib/saaj.jar org.apache.axis.wsdl.WSDL2Java partner.wsdl

$ javac -classpath ./:/usr/local/axis-1_4/lib/axis-ant.jar:/usr/local/axis-1_4/lib/commons-discovery-0.2.jar:/usr/local/axis-1_4/lib/jaxrpc.jar:/usr/local/axis-1_4/lib/wsdl4j-1.5.1.jar:/usr/local/axis-1_4/lib/axis.jar:/usr/local/axis-1_4/lib/commons-logging-1.0.4.jar:/usr/local/axis-1_4/lib/log4j-1.2.8.jar:/usr/local/axis-1_4/lib/saaj.jar com/sforce/soap/partner/*.java

$ find com/ | grep "class$" | xargs jar -cvf partner.jar

$ javac -classpath ./:/usr/local/axis-1_4/lib/axis-ant.jar:/usr/local/axis-1_4/lib/commons-discovery-0.2.jar:/usr/local/axis-1_4/lib/jaxrpc.jar:/usr/local/axis-1_4/lib/wsdl4j-1.5.1.jar:/usr/local/axis-1_4/lib/axis.jar:/usr/local/axis-1_4/lib/commons-logging-1.0.4.jar:/usr/local/axis-1_4/lib/log4j-1.2.8.jar:/usr/local/axis-1_4/lib/saaj.jar HelloAccount.java

$ java -classpath ./:/usr/local/axis-1_4/lib/axis-ant.jar:/usr/local/axis-1_4/lib/commons-discovery-0.2.jar:/usr/local/axis-1_4/lib/jaxrpc.jar:/usr/local/axis-1_4/lib/wsdl4j-1.5.1.jar:/usr/local/axis-1_4/lib/axis.jar:/usr/local/axis-1_4/lib/commons-logging-1.0.4.jar:/usr/local/axis-1_4/lib/log4j-1.2.8.jar:/usr/local/axis-1_4/lib/saaj.jar HelloAccount
import com.sforce.soap.partner.LoginResult;
import com.sforce.soap.partner.QueryResult;
import com.sforce.soap.partner.SessionHeader;
import com.sforce.soap.partner.SforceServiceLocator;
import com.sforce.soap.partner.SoapBindingStub;
import com.sforce.soap.partner.sobject.SObject;

public class HelloAccount {
  public static void main(String[] args) {
    try {
      SoapBindingStub binding = (SoapBindingStub) new SforceServiceLocator().getSoap();
      LoginResult loginResult = binding.login(<user name>, <password>);
      
      SessionHeader sh = new SessionHeader();
      sh.setSessionId(loginResult.getSessionId());      
      binding._setProperty(SoapBindingStub.ENDPOINT_ADDRESS_PROPERTY, loginResult.getServerUrl());
      binding.setHeader(new SforceServiceLocator().getServiceName().getNamespaceURI(), "SessionHeader", sh);
  
      QueryResult queryResult = binding.query("SELECT Id, Name FROM Account");
      for (SObject sobj : queryResult.getRecords()) {
        System.out.println(sobj.getId() + ", " + sobj.get_any()[1].getValue());
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

Hello Account (Java with WSC)

Java (WSC) で Hello Account。

$ wget https://sfdc-wsc.googlecode.com/files/wsc-23.jar
$ java -classpath wsc-23.jar com.sforce.ws.tools.wsdlc partner.wsdl partner-wsc.jar
$ javac -classpath ./:wsc-23.jar:partner-wsc.jar HelloAccount.java
$ java -classpath ./:wsc-23.jar:partner-wsc.jar HelloAccount
import com.sforce.soap.partner.*;
import com.sforce.soap.partner.sobject.SObject;
import com.sforce.ws.ConnectionException;
import com.sforce.ws.ConnectorConfig;
import java.io.FileNotFoundException;

public class HelloAccount {
  private PartnerConnection connection;
  
  private String authEndpoint = "https://test.salesforce.com/services/Soap/u/30.0";
  private String userName = <user name>;
  private String password = <password>;
  private String query = "SELECT Id, Name FROM Account";
  
  public static void main(String[] args) throws FileNotFoundException {
    HelloAccount helloAcct = new HelloAccount();
    if (helloAcct.login()) {
      helloAcct.getAccounts();
    }
  }
  
  private boolean login() throws FileNotFoundException {
    boolean success = false;
    ConnectorConfig config = new ConnectorConfig();
    config.setUsername(userName);
    config.setPassword(password);
    config.setAuthEndpoint(authEndpoint);
    config.setTraceFile("trace.log");
    config.setTraceMessage(true);
    config.setPrettyPrintXml(true);
    try {
      connection = Connector.newConnection(config);
      success = true;
    } catch (ConnectionException e) {
      e.printStackTrace();
    }
    return success;
  }
  
  private void getAccounts() {
    QueryResult queryResult;
    try {
      queryResult = connection.query(query);
      for (SObject sobj : queryResult.getRecords()) {
        System.out.println(sobj.getId() + ", " + sobj.getField("Name"));
      }
    } catch (ConnectionException e) {
      e.printStackTrace();
    }
  }
}

Hello Account (REST using cURL)

cURL で Hello Account。

$ curl -k -d grant_type=password -d client_id= -d client_secret= -d username= -d password= https://test.salesforce.com/services/oauth2/token -H 'X-PrettyPrint:1'

$ curl https://cs6.salesforce.com/services/data/v31.0/query?q=SELECT+Id+,+Name+FROM+Account -H 'Authorization: Bearer ' -H 'Content-Type: application/json' -H 'X-PrettyPrint:1'

Hello Account (JavaScript Remoting)

JavaScript Remoting で Hello Account。

<apex:page controller="HelloAccountRemoter">
    
<script type="text/javascript">
  function doGetAccounts() {
      HelloAccountRemoter.getAccounts(
        function(result, event) {
            for (var i = 0; i < result.length; i++) {
                console.log(result[i].Id, result[i].Name);
            }
        },
        {escape: true}
      );
  } 
</script>
    
<apex:form id="formId">
	<apex:commandButton onclick="doGetAccounts()" value="hello" rerender="formId" />
</apex:form>
    
</apex:page>

Hello Account (Ajax Toolkit)

Ajax Toolkit で Hello Account。

<apex:page >

<script src="/soap/ajax/31.0/connection.js" type="text/javascript"></script>
<script src="/soap/ajax/31.0/apex.js" type="text/javascript"></script>  

<script type="text/javascript">
  function getAccounts() {
    sforce.connection.sessionId = "{!$Api.Session_ID}";
    var accounts = sforce.connection.query("SELECT Id, Name FROM Account").getArray("records");
    for (var i = 0; i < accounts.length; i++) {
      console.log(accounts[i].Id, accounts[i].Name);
    }
  }
</script>

<apex:form >
    <apex:commandButton value="hello" onclick="getAccounts();"/>
</apex:form>

</apex:page>