search()

Executes a text search in your organization’s data.

Syntax

SearchResult = sdfc.search(String searchString);

Usage

Use search() to search for objects based on a search string. The search() call supports searching custom object. 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, its object must be configured as searchable (sSearchable 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.

Sample Code—Java

private void searchSample() {
  SearchResult sr = null;
  try {
    //    This search will look for a particular phone number in contacts,
    //    leads, and Accounts, returning similar information for contact
    //    and Leads and just the name and phone number for the accounts
    sr = binding.search("find {4159017000} " + "in phone fields " +
        "returning " + "contact(id, phone, firstname, lastname), " +
        "lead(id, phone, firstname, lastname), " +
        "account(id, phone, name)");
    //    Put the results into an array of SearchRecords
    SearchRecord[] records = sr.getSearchRecords();
    //    Check the length of the returned array of records to see
    //    if the search found anything
    if (records != null && records.length > 0) {
      //    We are going to use vectors to hold the results
      Vector contacts = new Vector();
      Vector leads = new Vector();
      Vector accounts = new Vector();
      //    We will go throught the results and determine what type
      //    of object we found by using instanceof and add each record
      //    to the correct vectory
      for (int i=0;i<records.length;i++){
        SObject record = records[i].getRecord();
        if (record instanceof Contact) {
          contacts.add(record);
        } else if (record instanceof Lead){
          leads.add(record);
        } else if (record instanceof Account) {
          accounts.add(record);
        }
      }
      //    we now have our results sorted into buckets of specific types
      //    so we can report our findings
      if (contacts.size() > 0) {
        System.out.println("Found " + new Integer(contacts.size()).toString() + 
                           " contacts:");
        for (int i=0;i<contacts.size();i++){
          Contact c = (Contact) contacts.get(i);
          System.out.println(c.getFirstName() + " " + c.getLastName() + " - " + 
                             c.getPhone());
        }
      }
      if (leads.size() > 0) {
        System.out.println("Found " + new Integer(leads.size()).toString() + " leads:");
        for (int i=0;i<leads.size();i++){
          Lead l = (Lead) leads.get(i);
          System.out.println(l.getFirstName() + " " + l.getLastName() + " - " + 
                             l.getPhone());
        }
      }
      if (accounts.size() > 0) {
        System.out.println("Found " + new Integer(accounts.size()).toString() + 
                           " accounts:");
        for (int i=0;i<accounts.size();i++){
          Account a = (Account) accounts.get(i);
          System.out.println(a.getName() + " - " + a.getPhone());
        }
      }
    } else {
      System.out.println("No records were found for the search.");
    }
  } catch (Exception ex) {
    System.out.println("An unexpected error has occurred." + ex.getMessage());
  }
}

Sample Code—C#

private void searchSample() 
{
    sr = binding.search("find {4159017000} " + 
        "in phone fields returning " +
        "contact(id, phone, firstname, lastname), " +
        "lead(id, phone, firstname, lastname), " +
        "account(id, phone, name)");

    sforce.SearchRecord[] records = sr.searchRecords;

    System.Collections.ArrayList contacts = new System.Collections.ArrayList();
    System.Collections.ArrayList leads = new System.Collections.ArrayList();
    System.Collections.ArrayList accounts = new System.Collections.ArrayList();

    if (records.Length > 0) 
    {
        for (int i=0;i<records.Length;i++)
        {
            sforce.sObject record = records[i].record;
            if (record.GetType() == typeof(sforce.Contact)) 
            {
                contacts.Add(record);
            } 
            else if (record.GetType() == typeof(sforce.Lead))
            {
                leads.Add(record);
            } 
            else if (record.GetType() == typeof(sforce.Account) )
            {
                accounts.Add(record);
            }
            System.Diagnostics.Trace.WriteLine("out");
        }
        if (contacts.Count > 0) 
        {
            Console.WriteLine("Found " + contacts.Count + " contacts:");
            for (int i=0;i<contacts.Count;i++)
            {
                sforce.Contact c = (sforce.Contact) contacts[i];
                Console.WriteLine(c.FirstName + " " + c.LastName + " - " + c.Phone);
            }
        }
        if (leads.Count > 0) 
        {
            Console.WriteLine("Found " + leads.Count + " leads:");
            for (int i=0;i<leads.Count;i++)
            {
                sforce.Lead l = (sforce.Lead) leads[i];
                Console.WriteLine(l.FirstName + " " + l.LastName + " - " + l.Phone);
            }
        }
        if (accounts.Count > 0) 
        {
            Console.WriteLine("Found " + accounts.Count + " accounts:");
            for (int i=0;i<accounts.Count;i++)
            {
                sforce.Account a = (sforce.Account) accounts[i];
                Console.WriteLine(a.Name + " - " + a.Phone);
            }
        }
    } 
    else 
    {
        Console.WriteLine("No records were found for the search.");        
    }
}

Arguments

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 objects to return. For more information, see Salesforce Object Search Language (SOSL).

Response

SearchResult

Fault

InvalidFieldFault

InvalidSObjectFault

MalformedSearchFault

UnexpectedErrorFault

See Also:
API Call Basics
https://wiki.apexdevnet.com/index.php/Sample_SOAP_Messages
© Copyright 2000-2008 salesforce.com, inc. All rights reserved.
Various trademarks held by their respective owners.