
- 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[] {'test@test.com'}; // 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('test@test.com'); 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);
廣告