- Apex 编程教程
- 顶点 - 主页
- Apex - 概述
- 顶点 - 环境
- 顶点 - 示例
- Apex - 数据类型
- Apex - 变量
- Apex - 弦乐
- Apex - 数组
- Apex - 常量
- Apex - 决策
- 顶点 - 循环
- Apex - 集合
- Apex - 课程
- Apex - 方法
- Apex - 对象
- Apex - 接口
- Apex-DML
- Apex - 数据库方法
- 顶点 - SOSL
- 顶点-SOQL
- 顶点 - 安全
- Apex - 调用
- Apex - 触发器
- Apex - 触发器设计模式
- Apex - 调节器限制
- Apex - 批处理
- Apex - 调试
- Apex - 测试
- Apex - 部署
- Apex 有用资源
- Apex - 快速指南
- Apex - 资源
- Apex - 讨论
顶点-SOQL
这是 Salesforce 对象查询语言,旨在与 SFDC 数据库配合使用。它只能在单个 sObject 中根据给定条件搜索记录。
与 SOSL 一样,它无法跨多个对象进行搜索,但它支持嵌套查询。
SOQL 示例
考虑一下我们正在进行的化学公司的例子。假设,我们需要今天创建的记录列表,其客户名称不是“test”。在这种情况下,我们必须使用 SOQL 查询,如下所示 -
// fetching the Records via SOQL
List<apex_invoice__c> InvoiceList = new List<apex_invoice__c>();
InvoiceList = [SELECT Id, Name, APEX_Customer__r.Name, APEX_Status__c FROM
APEX_Invoice__c WHERE createdDate = today AND APEX_Customer__r.Name != 'Test'];
// SOQL query for given criteria
// Printing the fetched records
System.debug('We have total '+InvoiceList.size()+' Records in List');
for (APEX_Invoice__c objInvoice: InvoiceList) {
System.debug('Record Value is '+objInvoice);
// Printing the Record fetched
}
您可以通过开发人员控制台中的查询编辑器运行 SOQL 查询,如下所示。
在开发者控制台中运行下面给出的查询。搜索今天创建的发票记录。
SELECT Id, Name, APEX_Customer__r.Name, APEX_Status__c FROM APEX_Invoice__c WHERE createdDate = today
您必须选择需要值的字段,否则可能会引发运行时错误。
遍历关系字段
这是 SFDC 中最重要的部分之一,因为很多时候我们需要遍历父子对象关系
此外,在某些情况下,您可能需要在数据库中插入两个关联的对象记录。例如,发票对象与客户对象有关系,因此一个客户可以拥有多张发票。
假设您正在创建发票,然后需要将此发票与客户相关联。您可以使用以下代码来实现此功能 -
// Now create the invoice record and relate it with the Customer object
// Before executing this, please create a Customer Records with Name 'Customer
// Creation Test'
APEX_Invoice__c objInvoice = new APEX_Invoice__c();
// Relating Invoice to customer via id field of Customer object
objInvoice.APEX_Customer__c = [SELECT id FROM APEX_Customer__c WHERE Name =
'Customer Creation Test' LIMIT 1].id;
objInvoice.APEX_Status__c = 'Pending';
insert objInvoice; //Creating Invoice
System.debug('Newly Created Invoice'+objInvoice); //Newly created invoice
在开发者控制台中执行此代码片段。执行后,从开发者控制台复制发票 ID,然后在 SFDC 中打开它,如下所示。您可以看到父记录已分配给发票记录,如下所示。
获取子记录
现在让我们考虑一个示例,其中与特定客户记录相关的所有发票都需要位于一个位置。为此,您必须知道子关系名称。要查看子关系名称,请转到子对象的字段详细信息页面并检查“子关系”值。在我们的示例中,它是在末尾附加 __r 的发票。
例子
在此示例中,我们需要设置数据,创建名为“ABC 客户”记录的客户,然后向该客户添加 3 张发票。
现在,我们将获取客户“ABC 客户”拥有的发票。以下是相同的查询 -
// Fetching Child Records using SOQL
List<apex_customer__c> ListCustomers = [SELECT Name, Id,
(SELECT id, Name FROM Invoices__r) FROM APEX_Customer__c WHERE Name = 'ABC Customer'];
// Query for fetching the Child records along with Parent
System.debug('ListCustomers '+ListCustomers); // Parent Record
List<apex_invoice__c> ListOfInvoices = ListCustomers[0].Invoices__r;
// By this notation, you could fetch the child records and save it in List
System.debug('ListOfInvoices values of Child '+ListOfInvoices);
// Child records
您可以在调试日志中查看记录值。
获取家长记录
假设您需要获取创建日期为今天的发票的客户名称,那么您可以使用下面给出的查询来实现相同的目的 -
例子
获取父记录的值以及子对象。
// Fetching Parent Record Field value using SOQL
List<apex_invoice__c> ListOfInvoicesWithCustomerName = new List<apex_invoice__c>();
ListOfInvoicesWithCustomerName = [SELECT Name, id, APEX_Customer__r.Name
FROM APEX_Invoice__c LIMIT 10];
// Fetching the Parent record's values
for (APEX_Invoice__c objInv: ListOfInvoicesWithCustomerName) {
System.debug('Invoice Customer Name is '+objInv.APEX_Customer__r.Name);
// Will print the values, all the Customer Records will be printed
}
这里我们使用了符号 APEX_Customer__r.Name,其中 APEX_Customer__r 是父关系名称,这里您必须在 Parent 字段的末尾附加 __r,然后才能获取父字段值。
聚合函数
SOQL 确实具有与 SQL 中一样的聚合函数。聚合函数使我们能够汇总和汇总数据。现在让我们详细了解该功能。
假设您想知道我们从客户“ABC Customer”那里获得的平均收入是多少,那么您可以使用此函数计算平均值。
例子
// Getting Average of all the invoices for a Perticular Customer
AggregateResult[] groupedResults = [SELECT
AVG(APEX_Amount_Paid__c)averageAmount FROM APEX_Invoice__c WHERE
APEX_Customer__r.Name = 'ABC Customer'];
Object avgPaidAmount = groupedResults[0].get('averageAmount');
System.debug('Total Average Amount Received From Customer ABC is '+avgPaidAmount);
检查调试日志中的输出。请注意,任何包含聚合函数的查询都会在AggregateResult对象数组中返回其结果。AggregateResult 是一个只读 sObject,仅用于查询结果。当我们需要生成大数据报告时,它很有用。
还有其他聚合函数可用于执行数据汇总。
MIN() - 这可用于查找最小值
MAX() - 这可用于查找最大值。
绑定 Apex 变量
您可以在 SOQL 查询中使用 Apex 变量来获取所需的结果。Apex 变量可以通过冒号 (:) 表示法引用。
例子
// Apex Variable Reference
String CustomerName = 'ABC Customer';
List<apex_customer__c> ListCustomer = [SELECT Id, Name FROM APEX_Customer__c
WHERE Name = :CustomerName];
// Query Using Apex variable
System.debug('ListCustomer Name'+ListCustomer); // Customer Name