Executes a text search in your organization’s data.
SearchResult = connection.search(String searchString);
Use search() to search for records based on a search string. The search() call supports searching custom objects. For an extensive discussion about the syntax and rules used for text searches, see Salesforce Object Search Language (SOSL).
Certain objects cannot be searched via the API, such as Attachment objects. To search an object via the search() call, the object must be configured as searchable (isSearchable is true). To determine whether an object can be searched, your client application can invoke the describeSObjects() call on the object and inspect its searchable property.
public void searchRecords() { try { String soslQuery = "FIND {4159017000} IN phone fields " + "returning contact(id, phone, firstname, lastname), " + "lead(id, phone, firstname, lastname), " + "account(id, phone, name)"; SearchResult sr = connection.search(soslQuery); SearchRecord[] records = sr.getSearchRecords(); if (records != null && records.length > 0) { List<SObject> objects = new ArrayList<SObject>(); for (SearchRecord record : records) { objects.add(record.getRecord()); } if (! objects.isEmpty()) { Iterator<SObject> iter = objects.iterator(); while (iter.hasNext()) { SObject record = iter.next(); if (record instanceof Account) { Account account = (Account) record; System.out.println("Account: " + account.getName() + " - " + account.getPhone() ); } else if (record instanceof Contact) { Contact contact = (Contact) record; System.out.println("Contact: " + contact.getFirstName() + " " + contact.getLastName() + " - " + contact.getPhone() ); } else /* Lead */ { Lead lead = (Lead) record; System.out.println("Lead: " + lead.getFirstName() + " " + lead.getLastName() + " - " + lead.getPhone() ); } } } } else { System.out.println("No records found for the search."); } } catch (ConnectionException ce) { ce.printStackTrace(); } }
using System;
using System.Collections.Generic;
using System.Text;
using SFDCWsdl.Salesforce_WSDL;
namespace SFDCWsdl
{
public class SFDCWsdlTest
{
private SforceService binding;
private static readonly string Username = "USERNAME";
private static readonly string Password = "PASSWORD";
/// <summary>
/// Create the binding and login
/// </summary>
private SFDCWsdlTest()
{
this.binding = new SforceService();
binding.login(SFDCWsdlTest.Username, SFDCWsdlTest.Password);
}
private void SearchSample()
{
SforceService binding = new SforceService();
SearchResult sr = null;
sr = binding.search("find {4159017000} in phone fields returning "
+ "contact(id, phone, firstname, lastname), "
+ "lead(id, phone, firstname, lastname), "
+ "account(id, phone, name)");
SearchRecord[] records = sr.searchRecords;
List<sObject> contacts = new List<sObject>();
List<sObject> leads = new List<sObject>();
List<sObject> accounts = new List<sObject>();
if (records.Length > 0)
{
for (int i = 0; i < records.Length; i++)
{
sObject record = records[i].record;
if (record.type.ToLower().Equals("contact"))
{
contacts.Add(record);
}
else if (record.type.ToLower().Equals("lead"))
{
leads.Add(record);
}
else if (record.type.ToLower().Equals("account"))
{
accounts.Add(record);
}
}
if (contacts.Count > 0)
{
Console.WriteLine("Found " + contacts.Count + " contacts:");
for (int i = 0; i < contacts.Count; i++)
{
sObject c = (sObject)contacts[i];
Console.WriteLine(c.Any[2].InnerText + " " +
c.Any[3].InnerText + " - " + c.Any[1].InnerText);
}
}
if (leads.Count > 0)
{
Console.WriteLine("Found " + leads.Count + " leads:");
for (int i = 0; i < leads.Count; i++)
{
sObject l = (sObject)leads[i];
Console.WriteLine(l.Any[2].InnerText + " " +
l.Any[3].InnerText + " - " + l.Any[1].InnerText);
}
}
if (accounts.Count > 0)
{
Console.WriteLine("Found " + accounts.Count + " accounts:");
for (int i = 0; i < accounts.Count; i++)
{
sObject a = (sObject)accounts[i];
Console.WriteLine(a.Any[2].InnerText + " - " +
a.Any[1].InnerText);
}
}
}
else
{
Console.WriteLine("No records were found for the search.");
}
}
}
}
| Name | Type | Description |
|---|---|---|
| search | string | Search string that specifies the text expression to search for, the scope of fields to search, the list of objects and fields to retrieve, and the maximum number of records to return. For more information, see Salesforce Object Search Language (SOSL). |