当前位置:K88软件开发文章中心编程语言非主流编程语言Apex → 文章内容

Apex - SOQL

减小字体 增大字体 作者:佚名  来源:网上搜集  发布时间:2019-1-15 16:30:54

由 kaikai0220 创建,youj 最后一次修改 2016-12-12 SOQL是为了使用SFDC数据库设计的 Salesforce 对象查询语言。它可以在单个sObject中在给定标准上搜索记录。 像SOSL一样,它不能搜索多个对象,但它支持嵌套查询。 SOQL示例想想我们正在进行的化学公司的例子。 假设,我们想有一个今天创建的记录列表,其客户名称不是'test'。 在这种情况下,我们将必须使用如下所示的SOQL查询: 例如: //fetching the Records via SOQLList<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 recordsSystem.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 objectobjInvoice.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 InvoiceSystem.debug('Newly Created Invoice'+objInvoice);//Newly creted invoice在开发者控制台中执行此代码段。 执行后,从开发人员控制台复制发票的Id,然后在SFDC中打开它,如下所示。 您可以看到父记录已分配给发票记录,如下所示。 获取子记录让我们举一个例子,我们希望所有的发票与特定的客户记录相关。 为此,您必须知道子关系名称。 要查看子关系名称,请转到子对象的字段详细信息页面,并检查“子关系”值。 在我们的示例中,它是由__r在末尾附加的发票。 例如: 首先,我们需要设置数据,创建一个名为“ABC客户”记录的客户,然后向该客户添加3张发票。现在,我们想获取客户的ABC客户有多少张发票。 下面是同样的查询://Fetching Child Records using SOQLList<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 ParentSystem.debug('ListCustomers '+ListCustomers);//Parent RecordList<apex_invoice__c> ListOfInvoices = ListCustomers[0].Invoices__r;//By this notation, you could fetch the child records and save it in ListSystem.debug('ListOfInvoices values of Childs '+ListOfInvoices);//Child records您可以在Debug日志中看到记录值。 获取父记录假设您要获取创建日期为今天的发票的客户名称,那么您可以使用以下查询: 例如:获取父记录的值以及子对象。//Fetching Parent Record Field value using SOQLList<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 valuesfor (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 CustomerAggregateResult[] 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 ReferenceString CustomerName = 'ABC Customer';List<apex_customer__c> ListCustomer = [SELECT Id, Name FROM APEX_Customer__c WHERE Name = :CustomerName];//Query Using Apex variableSystem.debug('ListCustomer Name'+ListCustomer);//Customer Name

Apex - SOQL