×
Apex 编程教程Apex 环境Apex 示例Apex 数据类型Apex 变量Apex 字符串Apex 数组Apex 常量Apex 决策Apex 循环Apex 集合Apex 类Apex 类方法Apex 对象Apex 接口Apex DMLApex 数据库方法Apex SOSLApex SOQLApex 安全性Apex 调用Apex 触发器Apex 触发设计模式Governer Limits调节器限制Apex 批量处理Apex 调试Apex 测试Apex 部署Apex 字符串Apex 数组

Apex SOQL


SOQL为了使用SFDC数据库设计的 Salesforce 对象查询语言它可以在单个sObject中在给定标准上搜索记录。


像SOSL一样,它不能搜索多个对象,但它支持嵌套查询。


SOQL示例

想想我们正在进行的化学公司的例子。 假设,我们想有一个今天创建的记录列表,其客户名称不是'test'。 在这种情况下,我们将必须使用如下所示的SOQL查询:


例如:

//fetching the Records via SOQL
List InvoiceList = new List();
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中最重要的部分之一,因为很多时候我们需要遍历父子对象关系。

此外,可能会需要在Database中插入两个关联对象记录。 例如,Invoice对象与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 creted invoice

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


遍历关系字段


获取子记录

让我们举一个例子,我们希望所有的发票与特定的客户记录相关。 为此,您必须知道子关系名称。 要查看子关系名称,请转到子对象的字段详细信息页面,并检查“子关系”值。 在我们的示例中,它是由__r在末尾附加的发票。


例如:

首先,我们需要设置数据,创建一个名为“ABC客户”记录的客户,然后向该客户添加3张发票。

现在,我们想获取客户的ABC客户有多少张发票。 下面是同样的查询:

//Fetching Child Records using SOQL
List 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 ListOfInvoices = ListCustomers[0].Invoices__r;//By this notation, you could fetch the child records and save it in List
System.debug('ListOfInvoices values of Childs '+ListOfInvoices);//Child records

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


获取父记录

假设您要获取创建日期为今天的发票的客户名称,那么您可以使用以下查询:


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

//Fetching Parent Record Field value using SOQL
List ListOfInvoicesWithCustomerName = new List();
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客户'获得的平均收入是多少,那么您可以使用此函数来占用平均值。


例如:

//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 ListCustomer = [SELECT Id, Name FROM APEX_Customer__c WHERE Name = :CustomerName];//Query Using Apex variable
System.debug('ListCustomer Name'+ListCustomer);//Customer Name


分类导航

关注微信下载离线手册

bootwiki移动版 bootwiki
(群号:472910771)