可能であれば、誰かが私を正しい方向に向けてくれませんか?2番目のコード(Batch Apex)はコンパイルされていません。現在、エラーは、
Error: Compile Error: Invalid type: updateContactOnEmailOptOutChangeScheduler
at line 63 column 73
しかし、私はそれを正しくすることができないように思われる他の問題があると思います。
連絡先の更新時に、このフィールドが更新されている場合、トリガーはすべての重複する連絡先を「Eメールオプトアウト」の新しい値で更新します。HasOptedOutOfEmail
また、トリガーは、更新されているものとは異なる値を持つ重複する連絡先のみを更新します。今の私の仕事は、この要件をトリガー(同僚によって作成およびテストされたもの)からBatchApexに変換することです。最初は元のトリガーです。2つ目は、バッチapexの形式で記述したコードです。
元のトリガーコード
trigger updateContactOnEmailOptOutChange on Contact (after update) {
//Initialize lists and maps
List<Contact> duplicateContacts = new List<Contact>();
Map<String, Contact> contactEmailMap = new Map<String, Contact>();
Map<Id, Contact> contactIdMap = new Map<Id, Contact>();
//Build a map with contacts to update. Only select the ones that have a different "Email Opt Out" value from the contact being updated.
for (Integer i = 0; i < Trigger.new.size(); i++) {
if (Trigger.old[i].HasOptedOutOfEmail != Trigger.new[i].HasOptedOutOfEmail) {
contactEmailMap.put(Trigger.old[i].email, Trigger.new[i]);
contactIdMap.put(Trigger.old[i].id, Trigger.new[i]);
}
}
//Only go through this process if "Email Opt Out" (HasOptedOutofEmail) was updated.
If (contactIdMap.size()>0) {
//Query the database and look for all contacts with a duplicate email address (same email as the contact currently being updated).
for (Contact dupContact : [SELECT Id, Name, Email, HasOptedOutOfEmail
FROM Contact
WHERE Email IN : contactEmailMap.KeySet()
AND Id NOT IN : contactIdMap.KeySet()]) {
Contact contact = contactEmailMap.get(dupContact.Email);
If (dupContact.HasOptedOutOfEmail <> contact.HasOptedOutOfEmail) {
dupContact.HasOptedOutOfEmail = contact.HasOptedOutOfEmail;
duplicateContacts.add(dupContact);
}
}
//If any duplicate contacts were found, update all duplicate contacts with the new HasOptedOutOfEmail value.
If (duplicateContacts.size()>0) update duplicateContacts;
}
}
バッチアペックス
global class updateContactOnEmailOptOutChange implements Database.Batchable<sObject>
{
global string query;
global updateContactOnEmailOptOutChange()
{
query = 'SELECT id,Name, Email, HasOptedOutofEmail from Contact where HasOptedOutofEmail=true';
}
global Database.QueryLocator start(Database.BatchableContext BC)
{
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List <sObject> duplicateContacts)
{
Map<String, Contact> contactEmailMap = new Map<String, Contact>();
Map <Id, Contact> contactIdMap = new Map<Id, Contact>();
// Build a map with contacts to update. Only select the ones that have a different "Email Opt Out" value from the contact being updated.
if(trigger.isUpdate){
for(Integer i=0; i<Trigger.new.size();i++)
{
if(Trigger.old[i].HasOptedOutOfEmail != Trigger.new[i].HasOptedOutOfEmail)
{
contactEmailMap.put(Trigger.old[i].email, Trigger.new[i]);
contactIdMap.put(Trigger.old[i].id, Trigger.new[i]);
}
}
if(contactidMap.size()>0)
{
//Query the database and look for all contacts with a duplicate email address(same email as the contact currently being updated)
for (Contact dupContact: [SELECT Id, Name, Email, HasOptedOutofEmail
FROM Contact
WHERE Email IN: contactEmailMap.KeySet()
AND Id NOT IN: contactIdMap.KeySet()])
{
Contact contact=contactEmailMap.get(dupContact.Email);
If(dupContact.HasOptedOutOfEmail <> contact.HasOptedOutOfEmail)
{
dupContact.HasOptedOutOfEmail = contact.HasOptedOutOfEmail;
duplicateContacts.add(dupContact);
}
}
// if any duplicate contacts were found, update all duplicate contacts with the new HasOptedOutOFEmail value.
If(duplicateContacts.size<>0) update duplicateContacts;
}
}
}
//The batch process has completed successfully. Schedule next batch.
global void finish(Database.BatchableContext BC){
// //Build the system time of now + 300 seconds to schedule the batch apex.
Datetime sysTime = System.now();
sysTime = sysTime.addSeconds(300);
String chron_exp=''+sysTime.second()+''+sysTime.minute()+''+sysTime.hour()+''+sysTime.day()+''+sysTime.month()+'?'+sysTime.year();
system.debug(chron_exp);
updateContactOnEmailOptOutChangeScheduler scheduleFieldUpdate = new updateContactOnEmailOptOutChangeScheduler();
//Schedule the next job, and give it the system time so name is unique
System.schedule('New Email Update Job'+sysTime.getTime(),chron_exp,scheduleFieldUpdate);
}
}