Apex-interfaces
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;
}
}
インターフェイスを実装する場合、そのインターフェイスのメソッドを実装することが必須です。 Interfaceメソッドを実装しないと、エラーがスローされます。 開発者にメソッドの実装を必須にする場合は、インターフェイスを使用する必要があります。
Apexの一括処理用の標準Salesforceインターフェイス
SFDCには、Database.Batchable、Schedulableなどの標準インターフェイスがあります。 たとえば、Database.Batchableインターフェイスを実装する場合、インターフェイスで定義されている3つのメソッド(Start、Execute、およびFinish)を実装する必要があります。
以下は、標準のSalesforceが提供するDatabase.Batchableインターフェースの例で、バッチステータスでユーザーにメールを送信します。 このインターフェイスには、Start、Execute、Finishの3つのメソッドがあります。 このインターフェイスを使用して、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);