You need to sign in to do that
Don't have an account?
lonedeveloper
Deleting large number of records in a trigger
Hi,
I am writing trigger to delete custom objects upon deletion of a Campaign.
Is there a limit of how many records can be deleted in a trigger?
I need to delete upto 300,000 records so wondering if this is possible to delete in a trigger. If not, is it possible to create Apex Job and execute in a trigger?
Thanks,
Batch apex works by splitting the entire data set into smaller chunks(batches) of a standard size (usually 200) records and then process each batch individually. Since the execution for each batch works on a small set of data, each batch would operate within the governor limits and when you put them all together, you would have achived processing above the normal limits that are applied. Please note that each execution for a batch still obeys the governor limits i.e. the execution for one batch of 200 records cannot query more than 50000 records, not more than 10000 DMLs, etc..
I think it is enough if you can use a scheduled job to clean up the records. The scheduled apex can be used to kickoff your batch apex. Using this scheduler, you can have the cleanup operations done ,say maybe once a day at midnight. Batch apex itself also has some limitations, like 5 million records and some others, but they are kind of generous so you wouldnt have to worry about them.
Thanks,
Jerun Jose
All Answers
Hi,
There is a governor limit in trigger. We can perform DML operation on 10000 records at a time.
For more detail follow the below link:
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_gov_limits.htm
We can call the apex job in trigger.
Try the below code snippet as reference:
trigger UpdateContactType on Account (after update)
{
Set<Id> SetAccountId = new Set<Id>();
for(Account objAccount: [Select Id from Account where Id in : Trigger.new])
{
SetAccountId.add(objAccount.Id);
}
if(!SetAccountId.isEmpty())
{
string strquery='SELECT Name ,Id ,UpdateContact_Type__c FROM Account Where Id IN :SetAccountId';
BatchToUpdateContactType objBatchToUpdateContactType=new BatchToUpdateContactType(strquery);
Database.executeBatch(objBatchToUpdateContactType);
}
}
Batch:
global class BatchToUpdateContactType implements Database.Batchable<sObject>, Database.stateful
{
global string strqry='';
global BatchToUpdateContactType(String q)
{
strqry=q;
}
global Database.Querylocator start (Database.Batchablecontext BC)
{
return Database.getQueryLocator(strqry);
}
global void execute (Database.Batchablecontext BC, list<sObject> scope)
{
// Perform your Action here
system.debug('All your Accounts are here'+scope);
}
global void finish(Database.Batchablecontext BC)
{
}
}
Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.
A partial answer (maybe): I think the governor limit prevents you even being able to retrieve that number of records (see http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_gov_limits.htm). You might be able to break the functionality down into smaller bits and use future methods (each with a separate context).
Alternativley, http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_scheduler.htm has the syntax for scheduling from apex.
I don't need to delete the objects in real time. I need to delete them when user deletes Campaign, if that happens at a later time then so be it.
Does Batch Apex have the same 10,000 DML and 50,000 max objects returned from SOQL queries limitations?
If not, I can create and queue Apex Job from within a trigger.
Batch apex works by splitting the entire data set into smaller chunks(batches) of a standard size (usually 200) records and then process each batch individually. Since the execution for each batch works on a small set of data, each batch would operate within the governor limits and when you put them all together, you would have achived processing above the normal limits that are applied. Please note that each execution for a batch still obeys the governor limits i.e. the execution for one batch of 200 records cannot query more than 50000 records, not more than 10000 DMLs, etc..
I think it is enough if you can use a scheduled job to clean up the records. The scheduled apex can be used to kickoff your batch apex. Using this scheduler, you can have the cleanup operations done ,say maybe once a day at midnight. Batch apex itself also has some limitations, like 5 million records and some others, but they are kind of generous so you wouldnt have to worry about them.
Thanks,
Jerun Jose
Thanks for your help.