public String createSample() { String result = null; try { // Create a new sObject of type Contact // and fill out its fields. SObject contact = new SObject(); contact.setType("Contact"); contact.setField("FirstName", "Otto"); contact.setField("LastName", "Jespersen"); contact.setField("Salutation", "Professor"); contact.setField("Phone", "(999) 555-1234"); contact.setField("Title", "Philologist"); // Add this sObject to an array SObject[] contacts = new SObject[1]; contacts[0] = contact; // Make a create call and pass it the array of sObjects SaveResult[] results = partnerConnection.create(contacts); // Iterate through the results list // and write the ID of the new sObject // or the errors if the object creation failed. // In this case, we only have one result // since we created one contact. for (int j = 0; j < results.length; j++) { if (results[j].isSuccess()) { result = results[j].getId(); System.out.println("\nA contact was created with an ID of: " + result); } else { // There were errors during the create call, // go through the errors array and write // them to the console for (int i = 0; i < results[j].getErrors().length; i++) { Error err = results[j].getErrors()[i]; System.out.println("Errors were found on item " + j); System.out.println("Error code: " + err.getStatusCode().toString()); System.out.println("Error message: " + err.getMessage()); } } } } catch (ConnectionException ce) { ce.printStackTrace(); } return result; }
public void updateSample(String id) { try { // Create an sObject of type contact SObject updateContact = new SObject(); updateContact.setType("Contact"); // Set the ID of the contact to update updateContact.setId(id); // Set the Phone field with a new value updateContact.setField("Phone", "(415) 555-1212"); // Create another contact that will cause an error // because it has an invalid ID. SObject errorContact = new SObject(); errorContact.setType("Contact"); // Set an invalid ID on purpose errorContact.setId("SLFKJLFKJ"); // Set the value of LastName to null errorContact.setFieldsToNull(new String[] {"LastName"}); // Make the update call by passing an array containing // the two objects. SaveResult[] saveResults = partnerConnection.update(new SObject[] {updateContact, errorContact}); // Iterate through the results and write the ID of // the updated contacts to the console, in this case one contact. // If the result is not successful, write the errors // to the console. In this case, one item failed to update. for (int j = 0; j < saveResults.length; j++) { System.out.println("\nItem: " + j); if (saveResults[j].isSuccess()) { System.out.println("Contact with an ID of " + saveResults[j].getId() + " was updated."); } else { // There were errors during the update call, // go through the errors array and write // them to the console. for (int i = 0; i < saveResults[j].getErrors().length; i++) { Error err = saveResults[j].getErrors()[i]; System.out.println("Errors were found on item " + j); System.out.println("Error code: " + err.getStatusCode().toString()); System.out.println("Error message: " + err.getMessage()); } } } } catch (ConnectionException ce) { ce.printStackTrace(); } }