课程表

Apex课程

工具箱
速查手册

Apex - SOSL

当前位置:免费教程 » 程序设计 » Apex
具有搜索功能是每个业务或应用程序的基本要求之一。为此,Salesforce.com提供了使用SOSL和SOQL的两种主要方法:


SOSL:

在整个对象和字段上搜索文本字符串将通过使用SOSL来完成。这是Salesforce对象搜索语言。它具有在多个对象上搜索特定字符串的能力。

SOSL语句评估sObjects的列表,其中每个列表包含特定sObject类型的搜索结果。结果列表始终以与在SOSL查询中指定的顺序相同的顺序返回。


SOQL:

这与SOQL几乎相同。您可以使用它从一个对象只获取一次对象记录。您可以编写嵌套查询,并从您要查询的父对象或子对象获取记录。

我们将在本章中学习SOSL。我们将在下一章探讨SOQL。


SOSL查询示例

考虑一个商业案例,我们需要开发一个程序,可以搜索指定的字符串。假设,我们想在Invoice对象的Customer Name字段中搜索字符串'ABC'。代码如下:

首先,您必须在Invoice对象中创建一个客户名为“ABC”的记录,以便在搜索时获得有效的结果。

  1. //Program To Search the given string in all Object
  2. //List to hold the returned results of sObject generic type
  3. List<list<SObject>> invoiceSearchList = new List<List<SObject>>();
  4.  
  5. //SOSL query which will search for 'ABC' string in Customer Name field of Invoice Object
  6. invoiceSearchList = [FIND 'ABC*' IN ALL FIELDS RETURNING APEX_Invoice__c (Id,APEX_Customer__r.Name)];
  7.  
  8. //Returned result will be printed
  9. System.debug('Search Result '+invoiceSearchList);
  10.  
  11. //Now suppose, you would like to search string 'ABC' in two objects, that is Invoice and Account. Then for this query goes like this:
  12. //Program To Search the given string in Invoice and Account object, you could specify more objects if you want, create an Account with Name as ABC.
  13. //List to hold the returned results of sObject generic type
  14. List<List<SObject>> invoiceAndSearchList = new List<List<SObject>>();
  15.  
  16. //SOSL query which will search for 'ABC' string in Invoice and in Account object's fields
  17. invoiceAndSearchList = [FIND 'ABC*' IN ALL FIELDS RETURNING APEX_Invoice__c (Id,APEX_Customer__r.Name), Account];
  18.  
  19. //Returned result will be printed
  20. System.debug('Search Result '+invoiceAndSearchList);
  21.  
  22. //This list will hold the returned results for Invoice Object
  23. APEX_Invoice__c [] searchedInvoice = ((List<APEX_Invoice__c>)invoiceAndSearchList[0]);
  24.  
  25. //This list will hold the returned results for Account Object
  26. Account [] searchedAccount = ((List<Account>)invoiceAndSearchList[1]);
  27. System.debug('Value of searchedInvoice'+searchedInvoice+'Value of searchedAccount'+searchedAccount);
转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号