zoukankan      html  css  js  c++  java
  • APEX初步 [6] —— SOSL查询

    Salesforce Object Search Language (SOSL)是Salesforce在记录中搜索文本的语言。可以用来在多个标准或自定义对象中搜索文本,类似Apache Lucene。

    可以在apex语句中直接嵌入SOSL语句,这种用法叫做Inline SOSL

    List<List<SObject>> searchList = [FIND 'SFDC' IN ALL FIELDS 
                                          RETURNING Account(Name), Contact(FirstName,LastName)];

    上面这句语句用来查询 任何字段中包含有‘SFDC’这个词的的客户和联系人。

    请注意在APEX中的搜索文本要用单引号‘SFDC’,在Query Editor中要用大括号{SFDC}

    SOQL ,SOSL之间的不同之处和相似之处

    • SOQL 用来从单个对象上获取记录
    • SOSL在多个对象上搜索文本字段

     下面是SOSL的标准格式:

    FIND 'SearchQuery' [IN SearchGroup] [RETURNING ObjectsAndFields]
    

     

    SearchQuery is the text to search for (a single word or a phrase). Search terms can be grouped with logical operators (AND, OR) and parentheses. Also, search terms can include wildcard characters (*, ?). The * wildcard matches zero or more characters at the middle or end of the search term. The ? wildcard matches only one character at the middle or end of the search term.

    Text searches are case-insensitive. For example, searching for Customercustomer, or CUSTOMER all return the same results.

    SearchGroup is optional. It is the scope of the fields to search. If not specified, the default search scope is all fields. SearchGroup can take one of the following values.

    • ALL FIELDS
    • NAME FIELDS
    • EMAIL FIELDS
    • PHONE FIELDS
    • SIDEBAR FIELDS

    ObjectsAndFields is optional. It is the information to return in the search result—a list of one or more sObjects and, within each sObject, list of one or more fields, with optional values to filter against. If not specified, the search results contain the IDs of all objects found.

    Search in all fields for:Search DescriptionMatched Records and Fields
    The Query This search returns all records whose fields contain both words: The and Query, in any location of the text. The order of words in the search term doesn’t matter. Account: The SFDC Query Man (Name field matched)
    Wingo OR Man This search uses the OR logical operator. It returns records with fields containing the Wingo word or records with fields containing the SFDC word. Contact: Carol Ruiz, Department: 'Wingo'

    Account: The SFDC Query Man (Name field matched)

    1212 This search returns all records whose fields contain the 1212 word. Phone fields that end with -1212 are matched because 1212 is considered a word when delimited by the dash. Account: The SFDC Query Man, Phone: '(415)555-1212'

    Contact: Carol Ruiz, Phone: '(415)555-1212'

    wing* This is a wildcard search. This search returns all records that have a field value starting with wing. Contact: Maria Ruiz, Department: 'Wingo'

    Account: The SFDC Query Man, Description: 'Expert in wing technologies.'

    List<List<sObject>> searchList = [FIND 'Wingo OR SFDC' IN ALL FIELDS 
                       RETURNING Account(Name),Contact(FirstName,LastName,Department)];
    Account[] searchAccounts = (Account[])searchList[0];
    Contact[] searchContacts = (Contact[])searchList[1];
    
    System.debug('Found the following accounts.');
    for (Account a : searchAccounts) {
        System.debug(a.Name);
    }
    
    System.debug('Found the following contacts.');
    for (Contact c : searchContacts) {
        System.debug(c.LastName + ', ' + c.FirstName);
    }
  • 相关阅读:
    一名菜鸟程序员的跳槽经历以及其所感所想(二)
    C#调用WebService
    IIS Error:404.2 The page you are requesting cannot be served because of the ISAPI and CGI Restriction list settings on the Web server
    C#操作XML简析系列(1)之增删修改查
    The web server process that was being debugged has been terminated by Internet Information Services (IIS).
    一名菜鸟程序员的跳槽经历以及其所感所想(一)
    访问WebService出现IIS错误:The request failed with HTTP status 401: Unauthorized
    Windows2008服务器搭建Apollo_MQTT服务
    [ObjC笔记] "self = [super init]"的解释与潜藏bug
    [LBS]查询离某个经纬附近的数据SQL语句
  • 原文地址:https://www.cnblogs.com/abovecloud/p/6506931.html
Copyright © 2011-2022 走看看