0

如果可能的话,有人可以指导我朝着正确的方向前进吗?第二个代码(Batch Apex)只是没有编译。目前的错误是,

Error: Compile Error: Invalid type: updateContactOnEmailOptOutChangeScheduler
                      at line 63 column 73

但我认为还有其他问题我似乎无法解决。

在联系人更新时,如果此字段已更新,则触发器会使用“电子邮件退出”的新值更新所有重复的联系人。此外,触发器仅更新具有HasOptedOutOfEmail与正在更新的值不同的值的重复联系人。现在我的任务是将这个需求从触发器(由我的同事编写和测试)转换为 Batch Apex。首先是原始触发器。二是我刚才写的batch apex格式的代码。

原始触发代码

trigger updateContactOnEmailOptOutChange on Contac​t (after update) {                                ​                                                  ​        

//Initialize lists and maps

List<Contact> duplicateContacts = new List<Con​tact>();
Map<String, Contact> contactEmailMap = new Map​&lt;String, Contact>();
Map<Id, Contact> contactIdMap = new Map<Id, Co​ntact>();

//Build a map with contacts to update. Only se​lect 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 != T​rigger.new[i].HasOptedOutOfEmail) {
        contactEmailMap.put(Trigger.old[i].ema​il, Trigger.new[i]);
        contactIdMap.put(Trigger.old[i].id, Tr​igger.new[i]);        
    }
}    

//Only go through this process if "Email Opt O​ut" (HasOptedOutofEmail) was updated.

If (contactIdMap.size()>0) {

    //Query the database and look for all cont​acts 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 <> c​ontact.HasOptedOutOfEmail) { 
            dupContact.HasOptedOutOfEmail = co​ntact.HasOptedOutOfEmail;   
            duplicateContacts.add(dupContact);
        }
    }    

    //If any duplicate contacts were found, up​date all duplicate contacts with the new HasOptedO​utOfEmail value.

   If (duplicateContacts.size()>0) update dupl​icateContacts;
}
}

批处理顶点

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.mont​h()+'?'+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,scheduleFieldUpda​te);
}
}
4

1 回答 1

0

您的批处理顶点包括对仅在触发器内有效的 Trigger 类的引用。编译错误消息说第 63 行存在无效类型。检查行号,但这可能指向对触发器的引用

于 2012-12-11T05:45:32.433 回答