- Apex编程教程
- Apex - 首页
- Apex - 概述
- Apex - 环境
- Apex - 示例
- Apex - 数据类型
- Apex - 变量
- Apex - 字符串
- Apex - 数组
- Apex - 常量
- Apex - 决策制定
- Apex - 循环
- Apex - 集合
- Apex - 类
- Apex - 方法
- Apex - 对象
- Apex - 接口
- Apex - DML
- Apex - 数据库方法
- Apex - SOSL
- Apex - SOQL
- Apex - 安全性
- Apex - 调用
- Apex - 触发器
- Apex - 触发器设计模式
- Apex - 总督限制
- Apex - 批处理
- Apex - 调试
- Apex - 测试
- Apex - 部署
- Apex有用资源
- Apex - 快速指南
- Apex - 资源
- Apex - 讨论
Apex - 接口
接口类似于Apex类,其中没有实现任何方法。它仅包含方法签名,但每个方法的主体都是空的。要使用接口,另一个类必须通过为接口中包含的所有方法提供主体来实现它。
接口主要用于为代码提供抽象层。它们将实现与方法的声明分开。
让我们以我们的化工公司为例。假设我们需要为高级客户和普通客户提供折扣,并且两者折扣不同。
我们将创建一个名为DiscountProcessor的接口。
// Interface public interface DiscountProcessor { Double percentageDiscountTobeApplied(); // method signature only } // Premium Customer Class public class PremiumCustomer implements DiscountProcessor { //Method Call public Double percentageDiscountTobeApplied () { // For Premium customer, discount should be 30% return 0.30; } } // Normal Customer Class public class NormalCustomer implements DiscountProcessor { // Method Call public Double percentageDiscountTobeApplied () { // For Premium customer, discount should be 10% return 0.10; } }
当您实现接口时,必须实现该接口的方法。如果您没有实现接口方法,它将抛出错误。当您希望使方法实现对开发人员强制执行时,应使用接口。
用于批处理Apex的标准Salesforce接口
SFDC确实有标准接口,例如Database.Batchable、Schedulable等。例如,如果您实现了Database.Batchable接口,则必须实现接口中定义的三个方法 – Start、Execute和Finish。
以下是以标准Salesforce提供的Database.Batchable接口为例,该接口向用户发送带有批处理状态的电子邮件。此接口有3个方法,Start、Execute和Finish。使用此接口,我们可以实现Batchable功能,它还提供BatchableContext变量,我们可以使用它来获取有关正在执行的批处理的更多信息并执行其他功能。
global class CustomerProessingBatch implements Database.Batchable<sobject7>, Schedulable { // Add here your email address global String [] email = new String[] {'[email protected]'}; // Start Method global Database.Querylocator start (Database.BatchableContext BC) { // This is the Query which will determine the scope of Records and fetching the same return Database.getQueryLocator('Select id, Name, APEX_Customer_Status__c, APEX_Customer_Decscription__c From APEX_Customer__c WHERE createdDate = today && APEX_Active__c = true'); } // Execute method global void execute (Database.BatchableContext BC, List<sobject> scope) { List<apex_customer__c> customerList = new List<apex_customer__c>(); List<apex_customer__c> updtaedCustomerList = new List<apex_customer__c>(); for (sObject objScope: scope) { // type casting from generic sOject to APEX_Customer__c APEX_Customer__c newObjScope = (APEX_Customer__c)objScope ; newObjScope.APEX_Customer_Decscription__c = 'Updated Via Batch Job'; newObjScope.APEX_Customer_Status__c = 'Processed'; // Add records to the List updtaedCustomerList.add(newObjScope); } // Check if List is empty or not if (updtaedCustomerList != null && updtaedCustomerList.size()>0) { // Update the Records Database.update(updtaedCustomerList); System.debug('List Size '+updtaedCustomerList.size()); } } // Finish Method global void finish(Database.BatchableContext BC) { Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); // get the job Id AsyncApexJob a = [Select a.TotalJobItems, a.Status, a.NumberOfErrors, a.JobType, a.JobItemsProcessed, a.ExtendedStatus, a.CreatedById, a.CompletedDate From AsyncApexJob a WHERE id = :BC.getJobId()]; System.debug('$$$ Jobid is'+BC.getJobId()); // below code will send an email to User about the status mail.setToAddresses(email); // Add here your email address mail.setReplyTo('[email protected]'); mail.setSenderDisplayName('Apex Batch Processing Module'); mail.setSubject('Batch Processing '+a.Status); mail.setPlainTextBody('The Batch Apex job processed '+a.TotalJobItems+'batches with '+a.NumberOfErrors+'failures'+'Job Item processed are'+a.JobItemsProcessed); Messaging.sendEmail(new Messaging.Singleemailmessage [] {mail}); } // Scheduler Method to scedule the class global void execute(SchedulableContext sc) { CustomerProessingBatch conInstance = new CustomerProessingBatch(); database.executebatch(conInstance,100); } }
要执行此类,您必须在开发人员控制台中运行以下代码。
CustomerProessingBatch objBatch = new CustomerProessingBatch (); Database.executeBatch(objBatch);
广告