You need to sign in to do that
Don't have an account?
Alex Meza
Converting a trigger into a batch class.
I wrote an apex trigger that connected two of my objects (Contacts and Voter File-custom object) via a look up field if they had the same first name, last name, and zipcode.
I was able to succesfully to this however, once I deployed to production I found out that my query was to large as I was trying to match across 15 million records or so.
After looking into I found out that I had to write an apex Batch class in order to have this work succesfully with my 15 million plus records, however now I am stuck.
I tried looking up some articles but I am not really sure what way to go such as if I have to start all over or just write the batch class based off the trigger I already wrote.
The trigger is below if you guys could help me with this I would be able to write the classes for my other triggers.
Thanks.
I was able to succesfully to this however, once I deployed to production I found out that my query was to large as I was trying to match across 15 million records or so.
After looking into I found out that I had to write an apex Batch class in order to have this work succesfully with my 15 million plus records, however now I am stuck.
I tried looking up some articles but I am not really sure what way to go such as if I have to start all over or just write the batch class based off the trigger I already wrote.
The trigger is below if you guys could help me with this I would be able to write the classes for my other triggers.
Thanks.
trigger Contact2VoterID on Contact (before insert,before update,after insert,after update) { { Set<String> set_Str = new Set<string>(); Map<String,Voter_File_TX__c> mp_VoterFile; if(Trigger.isAfter && Trigger.isUpdate) { for(Voter_File_TX__c VoterFile : [Select ID,First_Name__c, Last_Name__c,Zipcode__c, Contact__c From Voter_File_TX__c]) { if(mp_VoterFile==null) mp_VoterFile = new Map<String,Voter_File_TX__c>(); mp_VoterFile.put(VoterFile.First_Name__c +''+VoterFile.Last_Name__c +''+VoterFile.Zipcode__c,VoterFile); } for(Contact ContactList : Trigger.new) { if(mp_VoterFile!=null && mp_VoterFile.containsKey(ContactList.FirstName+''+ ContactList.LastName+''+ ContactList.MailingPostalCode)) { mp_VoterFile.get(ContactList.FirstName +''+ ContactList.LastName +''+ ContactList.MailingPostalCode).Contact__c = ContactList.id; } } if(mp_VoterFile!=null && mp_VoterFile.values()!=null) update mp_VoterFile.values(); } } }
or you can schedule it.
Dont hesitate to contact me if you have any question.
Best Regards.
All Answers
Trigger would be the ideal solution for you, however, you have to figure out ways to reduce the size of the dataset you have to work with.
For example, in the code below, I have achieved dataset size reduction by following two simple steps.
1. Filtered down the Voter_File_TX__c dataset by filtering the records using the MailingPostalCode in trigger.new.
2. Instead of updating mp_VoterFile (which contains quite a lot of records), I have taken the updated Voter_File_TX__c records into a smaller list. Hope this helps.
Feel free to improve the code further.
or you can schedule it.
Dont hesitate to contact me if you have any question.
Best Regards.
It says there is an issue with line 18 i got this error.
Failed to process batch for class 'UpdateContactsVoter' for job id '7076300000IyLT1'
caused by: System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Contact.FirstName
Class.UpdateContactsVoter.execute: line 18, column 1
Not sure where the issue is here because this is code worked in my trigger i first posted, do you have any thoughts on this?
If you observe Ahmad's code, he left the SOQL query that fetches Contacts, open ended, for you to update.
Please update line 3 of the code in such a way that it will fetch all the required fields from Contacts.
Please change to something like this... This will ensure that the error doesn't show up.
However, please make sure you put the right WHERE clause to restrict it from fetching all the records unnecessarily.
Hope this helps.
as LBK said, you should update the query and select the fields that you want to use, like firstname and lastname and any other field. And as he said dont forget to limit the query by using wehere clause so you only update the contacts that you want to update.
Please mark my previous answer as solution by selecting it as best answer if this solves your problem, So that if anyone has this issue this post can help.
Please help me to write a batch apex in order to have this code work for thousands of records.
trigger StageABC on abc__c (after insert) {
List<String> storeName = new List<String>();
List<String> storeNumber = new List<String>();
for (abc__c sABC : trigger.new) {
storeName.add(sABC.Store__c);
storeNumber.add(sABC.Store_Number__c);
}
List<xyz__c> xyzLoc = [
SELECT Id, ZipCode__c,Status__c,Store_Number__c,Store__c, FROM xyz__c
WHERE Store__c IN: storeName
AND Store_Number__c IN: storeNumber
];
Set<String> uniqueSet = new Set<String>();
for(xyz__c loc: xyzs) {
uniqueSet.add(loc.Store__c+ loc.Store_Number__c);
}
List<xyz__c> insertXYZ = new List<xyz__c>();
for (Staging__c sABC : trigger.new) {
if (!uniqueSet.contains(sABC.Store__c + sABC.Store_Number__c)) {
xyz__c loc = new xyz__c();
loc.Store__c = sABC.Store__c;
loc.Store_Number__c = sABC.Store_Number__c;
loc.ZipCode__c = sABC.ZipCode__c;
loc.Status__c = 'Active';
insertXYZ.add(loc);
}
}
if (! insertXYZ.isEmpty()) {
insert insertXYZ;
}
}