Guest User

Untitled

a guest
Feb 2nd, 2018
318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.73 KB | None | 0 0
  1. import com.sforce.soap.enterprise.Connector;
  2. import com.sforce.soap.enterprise.DeleteResult;
  3. import com.sforce.soap.enterprise.EnterpriseConnection;
  4. import com.sforce.soap.enterprise.Error;
  5. import com.sforce.soap.enterprise.QueryResult;
  6. import com.sforce.soap.enterprise.SaveResult;
  7. import com.sforce.soap.enterprise.sobject.Lead;
  8. import com.sforce.ws.ConnectorConfig;
  9. import com.sforce.ws.ConnectionException;
  10.  
  11. public class Main {
  12. static final String USERNAME = "sumit20112017@bisp.com";
  13. static final String PASSWORD = "Admin1234561KheL0lGSp1AAC0YKnnzdl403";
  14. static EnterpriseConnection connection;
  15. public static void main(String[] args)
  16. {
  17. ConnectorConfig config = new ConnectorConfig();
  18. config.setUsername(USERNAME);
  19. config.setPassword(PASSWORD);
  20. try {
  21. connection = Connector.newConnection(config);
  22. // display some current settings
  23. System.out.println("Auth EndPoint:"+config.getAuthEndpoint());
  24. System.out.println("Service EndPoint:"+config.getServiceEndpoint());
  25. System.out.println("Username: "+config.getUsername());
  26. System.out.println("SessionId: "+config.getSessionId());
  27. System.out.println("Connected Succesfully");
  28. queryLeads(); // Query Leads from Salesforce
  29. createLeads(); // Create Leads in Salesforce
  30. updateLeads(); // Update Leads in Salesforce
  31. deleteLeads(); // Delete Leads in Salesforce
  32. } catch (ConnectionException e1) {
  33. e1.printStackTrace();
  34. }
  35.  
  36.  
  37. try {
  38. connection.logout();
  39. System.out.println("Logged out.");
  40. } catch (ConnectionException ce) {
  41. ce.printStackTrace();
  42. }
  43.  
  44. }
  45.  
  46. private static void queryLeads() {
  47.  
  48. System.out.println("Querying for the 5 newest Leads...");
  49.  
  50. try {
  51.  
  52. // query for the 5 newest Leads
  53. QueryResult queryResults = connection.query("SELECT Id, FirstName, LastName, Company FROM Lead ORDER BY CreatedDate DESC LIMIT 5");
  54. if (queryResults.getSize() > 0) {
  55. for (int i=0;i<5;i++){
  56. // cast the SObject to a strongly-typed Lead
  57. Lead l = (Lead)queryResults.getRecords()[i];
  58. System.out.println("Id: " + l.getId() + " - Name: "+ l.getFirstName()+" "+ l.getLastName()+" - Company: "+l.getCompany());
  59. }
  60. }
  61.  
  62. } catch (Exception e) {
  63. e.printStackTrace();
  64. }
  65.  
  66. }
  67.  
  68. // create 5 test Leads
  69. private static void createLeads() {
  70.  
  71. System.out.println("Creating 5 new test Leads...");
  72. Lead[] records = new Lead[5];
  73.  
  74. try {
  75.  
  76. // create 5 test leads
  77. for (int i=0;i<5;i++) {
  78. Lead l = new Lead();
  79. l.setFirstName("SOAP API");
  80. l.setLastName("Lead "+i);
  81. l.setCompany("Bisp.com");
  82.  
  83. records[i] = l;
  84. }
  85.  
  86. // create the records in Salesforce.com
  87. SaveResult[] saveResults = connection.create(records);
  88.  
  89. // check the returned results for any errors
  90. for (int i=0; i< saveResults.length; i++) {
  91. if (saveResults[i].isSuccess()) {
  92. System.out.println(i+". Successfully created record - Id: " + saveResults[i].getId());
  93. } else {
  94. Error[] errors = saveResults[i].getErrors();
  95. for (int j=0; j< errors.length; j++)
  96. {
  97. System.out.println("ERROR creating record: " + errors[j].getMessage());
  98. }
  99. }
  100. }
  101. }
  102. catch (Exception e)
  103. { e.printStackTrace();
  104. }
  105.  
  106. }
  107.  
  108. // updates the 5 newly created Leads
  109. private static void updateLeads()
  110. {
  111. System.out.println("Update the 5 new test leads...");
  112. Lead[] records = new Lead[5];
  113. try {
  114. QueryResult queryResults = connection.query("SELECT Id, FirstName, LastName, Company FROM Lead ORDER BY CreatedDate DESC LIMIT 5");
  115. if (queryResults.getSize() > 0)
  116. {
  117. for (int i=0;i<5;i++){
  118. // cast the SObject to a strongly-typed Lead
  119. Lead l = (Lead)queryResults.getRecords()[i];
  120. System.out.println("Updating Id: " + l.getId() + " - Name: "+l.getFirstName()+" "+l.getLastName());
  121. // modify the name of the Lead
  122. l.setLastName(l.getLastName()+" -- UPDATED");
  123. records[i] = l;
  124. }
  125. }
  126.  
  127. // update the records in Salesforce.com
  128. SaveResult[] saveResults = connection.update(records);
  129.  
  130. // check the returned results for any errors
  131. for (int i=0; i< saveResults.length; i++) {
  132. if (saveResults[i].isSuccess()) {
  133. System.out.println(i+". Successfully updated record - Id: " + saveResults[i].getId());
  134. } else {
  135. Error[] errors = saveResults[i].getErrors();
  136. for (int j=0; j< errors.length; j++) { System.out.println("ERROR updating record: " + errors[j].getMessage()); } } } } catch (Exception e) { e.printStackTrace(); } }
  137.  
  138. // delete the 2 newly created Leads
  139. private static void deleteLeads() { System.out.println("Deleting the 2 new test Leads...");
  140. String[] ids = new String[2];
  141. try {
  142. QueryResult queryResults = connection.query("SELECT Id, Name FROM Lead ORDER BY CreatedDate DESC LIMIT 2");
  143. if (queryResults.getSize() > 0) {
  144. for (int i=0;i<queryResults.getRecords().length;i++) {
  145. // cast the SObject to a strongly-typed Lead
  146. Lead l = (Lead)queryResults.getRecords()[i];
  147. // add the Lead Id to the array to be deleted
  148. ids[i] = l.getId();
  149. System.out.println("Deleting Id: " + l.getId() + " - Name: "+l.getFirstName()+" "+l.getLastName());
  150. }
  151. }
  152.  
  153. // delete the records in Salesforce.com by passing an array of Ids
  154. DeleteResult[] deleteResults = connection.delete(ids);
  155.  
  156. // check the results for any errors
  157. for (int i=0; i< deleteResults.length; i++) {
  158. if (deleteResults[i].isSuccess()) {
  159. System.out.println(i+". Successfully deleted record - Id: " + deleteResults[i].getId());
  160. } else {
  161. Error[] errors = deleteResults[i].getErrors();
  162. for (int j=0; j< errors.length; j++) {
  163. System.out.println("ERROR deleting record: " + errors[j].getMessage());
  164. }
  165. }
  166. }
  167.  
  168. } catch (Exception e) {
  169. e.printStackTrace();
  170. }
  171.  
  172. }
  173.  
  174.  
  175.  
  176. }
Add Comment
Please, Sign In to add comment