You are on page 1of 4

package dev502;

import com.sforce.soap.enterprise.Connector;
import com.sforce.soap.enterprise.EnterpriseConnection;
import com.sforce.soap.enterprise.Error;
import com.sforce.soap.enterprise.QueryResult;
import com.sforce.soap.enterprise.SaveResult;
import com.sforce.soap.enterprise.sobject.Candidate__c;
import com.sforce.soap.enterprise.sobject.SObject;
import com.sforce.ws.ConnectionException;
import com.sforce.ws.ConnectorConfig;
/*
* This class exercises the following functionality of the Web Services API
* using the enterprise.wsdl:
* - Login
* - Query
* - Update
*/
public class QueryAndUpdateCandidateApp {
static final String USERNAME = ""; // TODO - (Exercise 4-1): salesforce.
com username
static final String PASSWORD = ""; // TODO - (Exercise 4-1): salesforce.
com password
static final String TOKEN = ""; // TODO - (Exercise 4-1): if outside of
trusted IP range, set salesforce.com token
// the variable that will contain the connection that will be used to ex
ecute all
// SalesForce Web Services API.
static EnterpriseConnection connection;
// class constructor
public QueryAndUpdateCandidateApp() {
// upon instantiation invoke process to perform the application
logic
doWork();
}
// this is the entry point into the application
public static void main(String[] args) {
// create a new instance of the class to begin processing
new QueryAndUpdateCandidateApp();
// output a final complete message
System.out.println("Application complete.");
}
// this method comprises the bulk of the application logic
private void doWork() {
//TODO (Exercise 4-2): Set doQuery to true.
boolean doQuery = false;
//TODO (Exercise 4-3): Set doUpdate to true.
boolean doUpdate = false;
if (doLogin() && doQuery) {
// perform a sample query and output candidate informati
on
Candidate__c candidate = doCandidateQuery();
// if a record was returned, proceed with update
if (candidate != null && doUpdate) {
// update candidate
doCandidateUpdate(candidate);
}
}
}
// this function encapsulates the logic necessary to login to salesforce
.com
private boolean doLogin() {
ConnectorConfig config = new ConnectorConfig();
config.setUsername(USERNAME);
//append token to password if needed.
config.setPassword(PASSWORD + TOKEN);
//config.setTraceMessage(true);
//If using a proxy to get through a firewall, uncomment the foll
owing code and set appropriate values.
//config.setProxy("proxyServer.corp.myCorp.com",8080);
//config.setNtlmDomain("NtlmDom");
//config.setProxyUsername("proxyUserName");
//config.setProxyPassword("***");
try {
connection = Connector.newConnection(config);
// display some current settings
System.out.println("Auth EndPoint: " + config.getAuthEnd
point());
System.out.println("Service EndPoint: " + config.getServ
iceEndpoint());
System.out.println("Username: " + config.getUsername());
System.out.println("SessionId: " + config.getSessionId()
);
System.out.println("Sforce service created.");
return true;
} catch (ConnectionException e1) {
e1.printStackTrace();
return false;
}
}
// this function encapsulates the logic necessary to query salesforce.co
m and
// output information about existing candidates
private Candidate__c doCandidateQuery() {
// declare local vars
QueryResult qr = null; // create a variable to hold the query re
sult
Candidate__c candidate = null; // create a variable to hold the
return value
/***************************************************************
********
* TODO - (Exercise 4-2): WRITE THE LOGIC NECESSARY TO QUERY AND
RETURN A SINGLE CANDIDATE
* 1. Declare a string to hold the SOQL query which will retriev
e the id, first name, last name of a single candidate
* 2. Using the connection obtained in the doLogin() method, exe
cute the query and obtain the result
* 3. This will need to be wrapped in a try-catch block. Simply
catch a generic Exception and print out the
* error message to the console.
***************************************************************
********/

/***************************************************************
********
* TODO - (Exercise 4-2):
* 4. Using the query result, obtain the candidate record from t
he first element in the list of records
* 5. Print out candidate's first and last name to the console.
***************************************************************
********/
// return the candidate
return candidate;
}
// this function encapsulates the logic necessary to update a received c
andidate
// within salesforce.com and output the results
private void doCandidateUpdate(Candidate__c candidate) {
// First we will create a candidate object array, all calls are
batch ours is a single element batch
Candidate__c[] candidates = new Candidate__c[1];
/***************************************************************
********
* TODO (Exercise 4-3) - WRITE THE LOGIC NECESSARY TO UPDATE THE
PHONE AND MOBILE FOR THE RECIEVED CANDIDATE
*
* 1. Set the phone and mobile fields on the candidate object
* 2. Add the candidate object to the array of candidates
***************************************************************
********/
// we are now ready to update the record
// create an array for the results
SaveResult[] saveResults = null;
/***************************************************************
********
* TODO (Exercise 4-3)
* 3. Using the connection object initialized in the doLogin met
hod, update the list of candidates and put the result in the saveResults array
* 4. This will need to be wrapped in a try-catch block. Simply
catch a generic Exception and print out the error message to the console.
***************************************************************
********/
for (SaveResult saveResult : saveResults) {
// check to see if the first update was a success
if (saveResult.isSuccess())
// the id that we passed should be the one we get back.
{
System.out.println("Updated Candidate with id: "
+ saveResult.getId());
} else {
// an error occurred on this record
System.out.println("Error: ");
Error error = saveResult.getErrors()[0];
System.out.println(error.getMessage());
}
}
}
}

You might also like