• Subhasmita Bhandari 24
  • NEWBIE
  • 0 Points
  • Member since 2020

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 1
    Replies
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;
    }
}
   
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. 
 
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();

    }
}
}