You need to sign in to do that
Don't have an account?
Subhasmita Bhandari 24
How to convert trigger into batch class?
Hi, I am facing a similar issue. I wrote an apex that connected two custom objects (abc and xyz) to find out the non identical xyz records from millions of abc records and add those new entries. For this I would be needing a batch class to run monthly.
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;
}
}
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;
}
}