private SalesForceSearchResponse findAccounts( String searchString, int pageSizeRequested, boolean reUpConnection) { if (salesForceConnection.get() == null) { LOGGER.error( "No Connection to SalesForce present, unable to process search request for '{}'", searchString); return new SalesForceSearchResponse(true, new ArrayList<SalesForce>()); } String strippedSearchString = StringUtils.stripToNull(searchString); // make sure a filter is there if (StringUtils.isBlank(strippedSearchString)) { return new SalesForceSearchResponse(true, new ArrayList<SalesForce>()); } int pageSize; if (pageSizeRequested <= 0) { pageSize = DEFAULT_PAGE_SIZE; } else { pageSize = pageSizeRequested; } List<SalesForce> accounts = new ArrayList<>(); int totalNumber; // perform a search for the term SearchResult searchResult = null; try { searchResult = salesForceConnection .get() .search(String.format(FIND_QUERY, escape(strippedSearchString), pageSize + 1)); } catch (ConnectionException e) { if (reUpConnection) { salesForceConnection.reUp(); findAccounts(searchString, pageSizeRequested, false); } else { LOGGER.error("SalesForce error", e); } } totalNumber = searchResult != null ? searchResult.getSearchRecords().length : 0; if (totalNumber > 0) { // found a match int count = 0; for (SearchRecord o : searchResult.getSearchRecords()) { accounts.add(convert(o.getRecord())); count++; if (count >= pageSize) { break; } } } return new SalesForceSearchResponse(totalNumber <= pageSize, accounts); }
public void searchSample(String phoneNumber) { try { // Example of phoneNumber format: 4155551212 String soslQuery = "FIND {" + phoneNumber + "} IN Phone FIELDS " + "RETURNING " + "Contact(Id, Phone, FirstName, LastName), " + "Lead(Id, Phone, FirstName, LastName)," + "Account(Id, Phone, Name)"; // Perform SOSL query SearchResult sResult = partnerConnection.search(soslQuery); // Get the records returned by the search result SearchRecord[] records = sResult.getSearchRecords(); // Create lists of objects to hold search result records List<SObject> contacts = new ArrayList<SObject>(); List<SObject> leads = new ArrayList<SObject>(); List<SObject> accounts = new ArrayList<SObject>(); // Iterate through the search result records // and store the records in their corresponding lists // based on record type. if (records != null && records.length > 0) { for (int i = 0; i < records.length; i++) { SObject record = records[i].getRecord(); if (record.getType().toLowerCase().equals("contact")) { contacts.add(record); } else if (record.getType().toLowerCase().equals("lead")) { leads.add(record); } else if (record.getType().toLowerCase().equals("account")) { accounts.add(record); } } // Display the contacts that the search returned if (contacts.size() > 0) { System.out.println("Found " + contacts.size() + " contact(s):"); for (SObject contact : contacts) { System.out.println( contact.getId() + " - " + contact.getField("FirstName") + " " + contact.getField("LastName") + " - " + contact.getField("Phone")); } } // Display the leads that the search returned if (leads.size() > 0) { System.out.println("Found " + leads.size() + " lead(s):"); for (SObject lead : leads) { System.out.println( lead.getId() + " - " + lead.getField("FirstName") + " " + lead.getField("LastName") + " - " + lead.getField("Phone")); } } // Display the accounts that the search returned if (accounts.size() > 0) { System.out.println("Found " + accounts.size() + " account(s):"); for (SObject account : accounts) { System.out.println( account.getId() + " - " + account.getField("Name") + " - " + account.getField("Phone")); } } } else { // The search returned no records System.out.println("No records were found for the search."); } } catch (ConnectionException ce) { ce.printStackTrace(); } }