目录

Apex - SOQL

这是专为与SFDC数据库一起使用而设计的Salesforce对象查询语言。 它只能在单个sObject中搜索给定条件的记录。

与SOSL一样,它不能跨多个对象进行搜索,但它确实支持嵌套查询。

SOQL示例

考虑我们正在进行的化学公司示例。 假设,我们需要一个今天创建的记录列表,其客户名称不是“测试”。 在这种情况下,我们将不得不使用下面给出的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
}

您可以通过Developer Console中的Query Editor运行SOQL查询,如下所示。

在Developer Console中运行下面给出的查询。 搜索今天创建的发票记录。

SELECT Id, Name, APEX_Customer__r.Name, APEX_Status__c FROM APEX_Invoice__c
   WHERE createdDate = today

您必须选择需要值的字段,否则会导致运行时错误。

遍历关系字段

这是SFDC中最重要的部分之一,因为我们需要遍历父子对象关系

此外,可能存在需要在数据库中插入两个关联对象记录的情况。 例如,Invoice对象与Customer对象有关系,因此一个Customer可以有很多发票。

假设您正在创建发票,然后您需要将此发票与客户相关联。 您可以使用以下代码实现此功能 -

// 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

在Developer Console中执行此代码段。 执行后,从开发人员控制台复制发票的ID,然后在SFDC中打开它,如下所示。 您可以看到父记录已分配给发票记录,如下所示。

获取父记录SOQL

获取儿童记录

现在让我们考虑一个例子,其中,与特定客户记录相关的所有发票都需要在一个地方。 为此,您必须知道子关系名称。 要查看子关系名称,请转到子对象的字段详细信息页面,然后选中“子关系”值。 在我们的示例中,最后是__r附加的发票。

例子 (Example)

在此示例中,我们需要设置数据,创建名为“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

您可以在Debug日志中看到Record值。

获取父记录

假设您需要获取创建日期为今天的发票客户名称,然后您可以使用下面给出的查询 -

例子 (Example)

获取父记录的值以及子对象。

// 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,然后你可以获取父字段值。

聚合函数 (Aggregate Functions)

SOQL确实具有我们在SQL中的聚合函数。 聚合函数允许我们汇总和汇总数据。 现在让我们详细了解这个功能。

假设您想知道我们从客户'ABC客户'获得的平均收入是多少,那么您可以使用此功能来获取平均收入。

例子 (Example)

// 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变量来获取所需的结果。 顶点变量可以通过冒号(:)表示法引用。

例子 (Example)

// 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
<上一篇.Apex - SOSL
↑回到顶部↑
WIKI教程 @2018