概述
Salesforce Object Search Language (SOSL)是Salesforce在记录中搜索文本的语言。可以用来在多个标准或自定义对象中搜索文本,类似Apache Lucene。
可以在apex语句中直接嵌入SOSL语句,这种用法叫做Inline SOSL
List> 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 Customer, customer, 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> 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);
}
最后
以上就是专一早晨为你收集整理的apex 查询_APEX初步 [6] —— SOSL查询的全部内容,希望文章能够帮你解决apex 查询_APEX初步 [6] —— SOSL查询所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复