Newer Version Available
Sample search Call
The following Java and C# examples show how to use the search() call for the partner WSDL. Each example accepts a phone number string value that is used in the SOQL query. The search call looks for phone fields that match the passed in phone value in all contacts, leads, and accounts. Next, the example iterates through the returned search results that contain the matching records, adds them to arrays, and writes their field values to the console. The record fields returned correspond to the fields specified in the SOQL query for each record type.
To execute the sample method, you can use the corresponding Java or C# template class provided in Examples Using the Partner WSDL.
Java Example
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}C# Example
1public void searchSample(String phoneNumber)
2{
3 try
4 {
5 // Example of phoneNumber format: 4155551212
6 String soslQuery =
7 "FIND {" + phoneNumber + "} IN Phone FIELDS " +
8 "RETURNING " +
9 "Contact(Id, Phone, FirstName, LastName), " +
10 "Lead(Id, Phone, FirstName, LastName)," +
11 "Account(Id, Phone, Name)";
12 // Perform SOSL query
13 SearchResult sResult = binding.search(soslQuery);
14 // Get the records returned by the search result
15 SearchRecord[] records = sResult.searchRecords;
16 // Create lists of objects to hold search result records
17 ArrayList contacts = new System.Collections.ArrayList();
18 ArrayList leads = new System.Collections.ArrayList();
19 ArrayList accounts = new System.Collections.ArrayList();
20
21 // Iterate through the search result records
22 // and store the records in their corresponding lists
23 // based on record type.
24 if ((records != null) && (records.Length > 0))
25 {
26 for (int i = 0; i < records.Length; i++)
27 {
28 sObject record = records[i].record;
29
30 if (record.type.ToLower().Equals("contact"))
31 {
32 contacts.Add(record);
33 }
34 else if (record.type.ToLower().Equals("lead"))
35 {
36 leads.Add(record);
37 }
38 else if (record.type.ToLower().Equals("account"))
39 {
40 accounts.Add(record);
41 }
42 }
43 // Display the contacts that the search returned
44 if (contacts.Count > 0)
45 {
46 Console.WriteLine("Found " + contacts.Count + " contact(s):");
47 for (int i = 0; i < contacts.Count; i++)
48 {
49 sObject c = (sObject)contacts[i];
50 Console.WriteLine(c.Any[0].InnerText + " - " +
51 c.Any[2].InnerText + " " +
52 c.Any[3].InnerText + " - " + c.Any[1].InnerText);
53 }
54 }
55 // Display the leads that the search returned
56 if (leads.Count > 0)
57 {
58 Console.WriteLine("Found " + leads.Count + " lead(s):");
59 for (int i = 0; i < leads.Count; i++)
60 {
61 sObject l = (sObject)leads[i];
62 Console.WriteLine(l.Any[0].InnerText + " - " +
63 l.Any[2].InnerText + " " +
64 l.Any[3].InnerText + " - " + l.Any[1].InnerText);
65 }
66 }
67 // Display the accounts that the search returned
68 if (accounts.Count > 0)
69 {
70 Console.WriteLine("Found " + accounts.Count + " account(s):");
71 for (int i = 0; i < accounts.Count; i++)
72 {
73 sObject a = (sObject)accounts[i];
74 Console.WriteLine(a.Any[0].InnerText + " - " +
75 a.Any[2].InnerText + " - " +
76 a.Any[1].InnerText);
77 }
78 }
79 }
80 else
81 {
82 // The search returned no records
83 Console.WriteLine("No records were found for the search.");
84 }
85 }
86 catch (SoapException e)
87 {
88 Console.WriteLine("An unexpected error has occurred: " + e.Message +
89 " Stack trace: " + e.StackTrace);
90 }
91}