Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import com.sforce.soap.enterprise.Connector;
- import com.sforce.soap.enterprise.EnterpriseConnection;
- import com.sforce.soap.enterprise.QueryResult;
- import com.sforce.soap.enterprise.SaveResult;
- import com.sforce.soap.enterprise.sobject.Contact;
- import com.sforce.soap.enterprise.sobject.Student__c;
- import com.sforce.ws.ConnectionException;
- import com.sforce.ws.ConnectorConfig;
- public class ExampleContact {
- static final String USERNAME = "pawan.manglani@metacube.com";
- static final String PASSWORD = "pawan7733923277EF1SJRIAz3KpMPwPLE1WKwEb";
- static EnterpriseConnection connection;
- public static void main(String[] args) {
- ConnectorConfig config = new ConnectorConfig();
- config.setUsername(USERNAME);
- config.setPassword(PASSWORD);
- try {
- connection = 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());
- } catch (ConnectionException e1) {
- e1.printStackTrace();
- }
- queryContacts();
- createStudents();
- }
- private static void queryContacts() {
- System.out.println("Querying for the 5 newest Contacts...");
- try {
- // query for the 5 newest contacts
- QueryResult queryResults = connection.query("SELECT Id, FirstName, LastName, Account.Name " +
- "FROM Contact WHERE AccountId != NULL ORDER BY CreatedDate DESC LIMIT 5");
- if (queryResults.getSize() > 0) {
- for (int i = 0; i < queryResults.getRecords().length; i++) {
- // cast the SObject to a strongly-typed Contact
- Contact cont = (Contact) queryResults.getRecords()[i];
- System.out.println("Id: " + cont.getId() + " - Name: " + cont.getFirstName() + " " +
- cont.getLastName() + " - Account: " + cont.getAccount().getName());
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- // create 5 test Accounts
- private static void createStudents() {
- System.out.println("Creating 2 new test Students...");
- // create 2 test students
- Student__c[] records = new Student__c[2];
- try {
- // create 2 test students
- for (int index = 0; index < 2; index++) {
- Student__c student = new Student__c();
- student.setFirst_Name__c(("My"));
- student.setLast_Name__c(("Student")+index);
- student.setClass__c("a007F000005bpR8");
- records[index] = student;
- }
- // create the records in Salesforce.com
- SaveResult[] saveResults = connection.create(records);
- // check the returned results for any errors
- for (int i = 0; i < saveResults.length; i++) {
- if (saveResults[i].isSuccess()) {
- System.out.println(i + ". Successfully created student record - Id: " + saveResults[i].getId());
- } else {
- com.sforce.soap.enterprise.Error[] errors = saveResults[i].getErrors();
- for (int j = 0; j < errors.length; j++) {
- System.out.println("ERROR creating student record: " + errors[j].getMessage());
- }
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
Add Comment
Please, Sign In to add comment