Note that there are some explanatory texts on larger screens.

plurals
  1. POConverting Trigger to Batch Apex: Help needed to make the code work
    text
    copied!<p>If possible, could anyone please direct me towards the right direction? The second code (Batch Apex) is just not compiling. Currently the error is,</p> <pre><code>Error: Compile Error: Invalid type: updateContactOnEmailOptOutChangeScheduler at line 63 column 73 </code></pre> <p>But I think there are other issues to that I can't seem to make it right. </p> <p>On a contact update, the trigger updates all duplicate contacts with new value of "Email Opt Out" if this field has been updated. Also, the trigger only updates duplicate contacts that have a <code>HasOptedOutOfEmail</code> value different from one being updated. Now my task is to convert this requirement from trigger (that was written and tested by my colleague) to Batch Apex. First is the original trigger. Second is the code I just wrote in the format of batch apex.</p> <p><strong>Original Trigger Code</strong></p> <pre><code>trigger updateContactOnEmailOptOutChange on Contac​t (after update) { ​ ​ //Initialize lists and maps List&lt;Contact&gt; duplicateContacts = new List&lt;Con​tact&gt;(); Map&lt;String, Contact&gt; contactEmailMap = new Map​&lt;String, Contact&gt;(); Map&lt;Id, Contact&gt; contactIdMap = new Map&lt;Id, Co​ntact&gt;(); //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 &lt; 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()&gt;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 &lt;&gt; 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()&gt;0) update dupl​icateContacts; } } </code></pre> <p><strong>BATCH APEX</strong></p> <pre><code>global class updateContactOnEmailOptOutChange implements Database.Batchable&lt;sObject&gt; { 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 &lt;sObject&gt; duplicateContacts) { Map&lt;String, Contact&gt; contactEmailMap = new Map&lt;String, Contact&gt;(); Map &lt;Id, Contact&gt; contactIdMap = new Map&lt;Id, Contact&gt;(); // 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&lt;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()&gt;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 &lt;&gt; 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&lt;&gt;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); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload