Sample Implementation

The code in Listing 11.19 is a sample Java implementation of the integration. It assumes that you’ve already generated the Java stub code from Enterprise WSDL using the WSC. It expects a file named import.json to be located in the working directory. This is a JSON-encoded file containing an array of Contact records to update. Listing 11.20 is an example of the file format expected by the program.


Note

The sample implementation uses a JSON library available at www.json.org/java.


Listing 11.19 Sample Java Implementation of Integration Scenario


import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.sforce.soap.enterprise.Connector;
import com.sforce.soap.enterprise.EnterpriseConnection;
import com.sforce.soap.enterprise.UpsertResult;
import com.sforce.soap.enterprise.sobject.Contact;
import com.sforce.soap.enterprise.sobject.SObject;
import com.sforce.ws.ConnectionException;
import com.sforce.ws.ConnectorConfig;
public class Listing11_19 {
  EnterpriseConnection connection;
  public void login(String user, String pass, String securityToken) {
    ConnectorConfig config = new ConnectorConfig();
    config.setUsername(user);
    config.setPassword(pass + securityToken);
    try {
      connection = Connector.newConnection(config);
    } catch (ConnectionException e) {
      e.printStackTrace();
    }
  }
  public void processImportFile(String jsonFile) {
    List<SObject> changes = new ArrayList<SObject>();
    try {
      String json = readFileAsString(jsonFile);
      JSONArray array = new JSONArray(json);
      for (int i=0; i<array.length(); i++) {
        changes.add(importResource(array.getJSONObject(i)));
      }
      if (changes.size() > 0) {
        UpsertResult[] results = connection.upsert("Resource_ID__c",
          changes.toArray(new SObject[changes.size()]));
        int line = 0;
        for (UpsertResult result : results) {
          System.out.print(line + ": ");
          if (!result.isSuccess()) {
            for (com.sforce.soap.enterprise.Error e
              : result.getErrors()) {
              System.out.println(e.getStatusCode() + ": " +
                e.getMessage());
            }
          } else {
            System.out.println("success");
          }
          line++;
        }
      }
    } catch (Throwable t) {
      t.printStackTrace();
    }
  }
  private Contact importResource(JSONObject rec)
    throws JSONException {
    Contact result = new Contact();
    result.setResource_ID__c(Double.valueOf(
      rec.getInt("ResourceID")));
    result.setActive__c(rec.getBoolean("Active"));
    return result;
  }
  private static String readFileAsString(String filePath)
    throws IOException {
    StringBuffer fileData = new StringBuffer(1000);
    BufferedReader reader = new BufferedReader(
      new FileReader(filePath));
    char[] buf = new char[2048];
    int numRead = 0;
    while((numRead = reader.read(buf)) != -1) {
      fileData.append(buf, 0, numRead);
    }
    reader.close();
    return fileData.toString();
  }
  public static void main(String[] args) {
    Listing11_19 demo = new Listing11_19();
    demo.login("USERNAME", "PASSWORD", "SECURITYTOKEN");
    demo.processImportFile("import.json");
  }
}


Listing 11.20 Sample JSON Input File


[
  {
    "ResourceID": 100000,
    "Active": false
  },
  {
    "ResourceID": 100001,
    "Active": false
  }
]


Before running the program, change the Resource ID values in the file to match your contacts, and the arguments of the login method to your user credentials.

Note that the only field updated by the sample implementation is Active__c. As a challenge, enhance the program to support updates to additional fields of the Contact object, or related objects like User.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.222.231