0

So here is the issue We are loading data into a CustomObject__c using DataLoader. Usually the no of records that are passed are 3. Also, if there is any issue with the data passed, they run the dataloader again and pass the corrected data. Now, the older data has to be deleted. So, I am handling it in before insert code and calling a batch in after insert code.

Here is the code for my trigger:

trigger TriggerCustom on CustomObject__c (before insert, after insert) {
  List<CustomObject__c> customobjectlist = [Select Id from CustomObject__c WHERE CreatedDate = TODAY ];
    if (Trigger.isBefore) {
        delete exchlisttoday;
        
    }
    if(Trigger.isAfter)
    {
         BatchApex b = BatchApex();    
            Database.executebatch(b);
    }
}

This was designed keeping in mind they pass only 3 records at a time. However, now they want to pass more than 200 records using data loader. How can I modify my trigger so that it fires only after one single dataload is completed (for e.g. if they pass 1000 records at once, the trigger has to fire only after the 1000 records are completely inserted

2

How can I modify my trigger so that it fires only after one single dataload is completed (for e.g. if they pass 1000 records at once, the trigger has to fire only after the 1000 records are completely inserted

You can't. Triggers don't work like that, because triggers are part of the insertion process (the save order). Your trigger will fire for each batch of 200 records inserted during a single transaction. Note that it may or may not be possible to insert 1000 records in one transaction at all, depending on how well-optimized your automation is; you may hit various governor limits.

Your trigger appears to be aiming to delete existing records when new ones are inserted. This is a risky pattern because it assumes that a data load fits within a single transaction, and moreso within a single trigger invocation. That latter point is no longer the case, so you'll have to change how you manage this process.

I would suggest you reframe your process to use a script or off-platform middleware solution to orchestrate the multi-stage operation, where you delete existing data, load new data, and then apparently process the new data with a batch job. You could also consider preprocessing the data off-platform to avoid the need for a batch job.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.