将触发器转换为批处理Apex:使代码工作所需的帮助

时间:2012-12-10 22:09:08

标签: batch-file triggers salesforce

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

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

但我认为还有其他问题我似乎无法做到正确。

在联系人更新中,如果此字段已更新,则触发器会使用新值“电子邮件退出”更新所有重复的联系人。此外,触发器仅更新重复的联系人,这些联系人的HasOptedOutOfEmail值与正在更新的联系人不同。现在我的任务是将此要求从触发器(由我的同事编写和测试)转换为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​<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;
}
}

BATCH APEX

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);
}
}

1 个答案:

答案 0 :(得分:0)

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