function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
lonedeveloperlonedeveloper 

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,

Best Answer chosen by Admin (Salesforce Developers) 
Jerun JoseJerun Jose

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

Navatar_DbSupNavatar_DbSup

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. 

stcforcestcforce

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.

Jerun JoseJerun Jose
You can perform a total DML of upto 10000 records in a trigger. This includes inserts, updates and deletes. In your case, I am honestly baffled as to why you would need to delete 300,000 records when a single campaign is deleted. Anyway if you want to do that realtime (somewhat realtime atleast), you would need to use a Batch apex. The batch apex will need to have the logic to identify the records that are to be deleted and delete them. WORD OF CAUTION : There is a limit of having only 5 batch processes running simultaneously in an organisation. So be very careful as to how you design your trigger as you have to be absolutely sure that no more than 5 instances of the batch apex will be running at any point of time. Thanks, Jerun Jose
lonedeveloperlonedeveloper

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.

 

Jerun JoseJerun Jose

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

This was selected as the best answer
lonedeveloperlonedeveloper

Thanks for your help.