Guest User

examplesalesforce.java

a guest
Jan 11th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.15 KB | None | 0 0
  1. import com.sforce.soap.enterprise.Connector;
  2. import com.sforce.soap.enterprise.EnterpriseConnection;
  3. import com.sforce.soap.enterprise.QueryResult;
  4. import com.sforce.soap.enterprise.SaveResult;
  5. import com.sforce.soap.enterprise.sobject.Contact;
  6. import com.sforce.soap.enterprise.sobject.Student__c;
  7. import com.sforce.ws.ConnectionException;
  8. import com.sforce.ws.ConnectorConfig;
  9.  
  10.  
  11.  
  12. public class ExampleContact {
  13.  
  14.  
  15.  
  16. static final String USERNAME = "pawan.manglani@metacube.com";
  17. static final String PASSWORD = "pawan7733923277EF1SJRIAz3KpMPwPLE1WKwEb";
  18. static EnterpriseConnection connection;
  19.  
  20. public static void main(String[] args) {
  21. ConnectorConfig config = new ConnectorConfig();
  22. config.setUsername(USERNAME);
  23. config.setPassword(PASSWORD);
  24. try {
  25. connection = Connector.newConnection(config);
  26.  
  27. System.out.println("Auth EndPoint: " + config.getAuthEndpoint());
  28. System.out.println("Service EndPoint: " + config.getServiceEndpoint());
  29. System.out.println("Username: " + config.getUsername());
  30. System.out.println("SessionId: " + config.getSessionId());
  31.  
  32. } catch (ConnectionException e1) {
  33. e1.printStackTrace();
  34. }
  35. queryContacts();
  36. createStudents();
  37. }
  38.  
  39. private static void queryContacts() {
  40.  
  41. System.out.println("Querying for the 5 newest Contacts...");
  42.  
  43. try {
  44.  
  45. // query for the 5 newest contacts
  46. QueryResult queryResults = connection.query("SELECT Id, FirstName, LastName, Account.Name " +
  47. "FROM Contact WHERE AccountId != NULL ORDER BY CreatedDate DESC LIMIT 5");
  48. if (queryResults.getSize() > 0) {
  49.  
  50. for (int i = 0; i < queryResults.getRecords().length; i++) {
  51.  
  52. // cast the SObject to a strongly-typed Contact
  53.  
  54. Contact cont = (Contact) queryResults.getRecords()[i];
  55.  
  56. System.out.println("Id: " + cont.getId() + " - Name: " + cont.getFirstName() + " " +
  57.  
  58. cont.getLastName() + " - Account: " + cont.getAccount().getName());
  59. }
  60. }
  61. } catch (Exception e) {
  62. e.printStackTrace();
  63. }
  64. }
  65.  
  66. // create 5 test Accounts
  67.  
  68. private static void createStudents() {
  69.  
  70. System.out.println("Creating 2 new test Students...");
  71.  
  72. // create 2 test students
  73.  
  74. Student__c[] records = new Student__c[2];
  75.  
  76. try {
  77. // create 2 test students
  78.  
  79. for (int index = 0; index < 2; index++) {
  80.  
  81. Student__c student = new Student__c();
  82.  
  83. student.setFirst_Name__c(("My"));
  84. student.setLast_Name__c(("Student")+index);
  85. student.setClass__c("a007F000005bpR8");
  86.  
  87. records[index] = student;
  88.  
  89. }
  90.  
  91. // create the records in Salesforce.com
  92.  
  93. SaveResult[] saveResults = connection.create(records);
  94.  
  95. // check the returned results for any errors
  96.  
  97. for (int i = 0; i < saveResults.length; i++) {
  98.  
  99. if (saveResults[i].isSuccess()) {
  100.  
  101. System.out.println(i + ". Successfully created student record - Id: " + saveResults[i].getId());
  102.  
  103. } else {
  104.  
  105. com.sforce.soap.enterprise.Error[] errors = saveResults[i].getErrors();
  106.  
  107. for (int j = 0; j < errors.length; j++) {
  108. System.out.println("ERROR creating student record: " + errors[j].getMessage());
  109. }
  110. }
  111. }
  112. } catch (Exception e) {
  113. e.printStackTrace();
  114. }
  115. }
  116. }
Add Comment
Please, Sign In to add comment