sforce logo

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 sforce Object Search Language (SOSL).

Certain objects cannot be searched via the sforce API, such as Attachment objects. To search an object via the search call, its object must be configured as searchable (searchable=True). To determine whether an object can be searched, your client application can invoke the describeSObject call on the object and inspect its searchable property.

Sample Code-Java

private void searchSample() { 
 
    SearchResult sr = null; 
    //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.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 = (SObject)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."); 
    } 
} 

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
searchString
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 sforce Object Search Language (SOSL).

Response

SearchResult

Fault

MalformedSearchFault

See Also

Concepts


© Copyright 2000-2003 SalesForce.com, Inc.
All rights reserved • Various trademarks held by their respective owners.