1public void searchSample(String phoneNumber) {
2 try {
3 // Example of phoneNumber format: 4155551212
4 String soslQuery =
5 "FIND {" + phoneNumber + "} IN Phone FIELDS " +
6 "RETURNING " +
7 "Contact(Id, Phone, FirstName, LastName), " +
8 "Lead(Id, Phone, FirstName, LastName)," +
9 "Account(Id, Phone, Name)";
10 // Perform SOSL query
11 SearchResult sResult = partnerConnection.search(soslQuery);
12 // Get the records returned by the search result
13 SearchRecord[] records = sResult.getSearchRecords();
14 // Create lists of objects to hold search result records
15 List<SObject> contacts = new ArrayList<SObject>();
16 List<SObject> leads = new ArrayList<SObject>();
17 List<SObject> accounts = new ArrayList<SObject>();
18
19 // Iterate through the search result records
20 // and store the records in their corresponding lists
21 // based on record type.
22 if (records != null && records.length > 0) {
23 for (int i = 0; i < records.length; i++){
24 SObject record = records[i].getRecord();
25 if (record.getType().toLowerCase().equals("contact")) {
26 contacts.add(record);
27 } else if (record.getType().toLowerCase().equals("lead")){
28 leads.add(record);
29 } else if (record.getType().toLowerCase().equals("account")) {
30 accounts.add(record);
31 }
32 }
33 // Display the contacts that the search returned
34 if (contacts.size() > 0) {
35 System.out.println("Found " + contacts.size() +
36 " contact(s):");
37 for (SObject contact : contacts) {
38 System.out.println(contact.getId() + " - " +
39 contact.getField("FirstName") + " " +
40 contact.getField("LastName") + " - " +
41 contact.getField("Phone")
42 );
43 }
44 }
45 // Display the leads that the search returned
46 if (leads.size() > 0) {
47 System.out.println("Found " + leads.size() +
48 " lead(s):");
49 for (SObject lead : leads) {
50 System.out.println(lead.getId() + " - " +
51 lead.getField("FirstName") + " " +
52 lead.getField("LastName") + " - " +
53 lead.getField("Phone")
54 );
55 }
56 }
57 // Display the accounts that the search returned
58 if (accounts.size() > 0) {
59 System.out.println("Found " +
60 accounts.size() + " account(s):");
61 for (SObject account : accounts) {
62 System.out.println(account.getId() + " - " +
63 account.getField("Name") + " - " +
64 account.getField("Phone")
65 );
66 }
67 }
68 } else {
69 // The search returned no records
70 System.out.println("No records were found for the search.");
71 }
72 } catch (ConnectionException ce) {
73 ce.printStackTrace();
74 }
75}