+ Start a Discussion
sfdcsushilsfdcsushil 

Trigger v/s Batch Apex - Best Approach

Hi,

 

We have requirement as follows:-

 

When a custom object is updated, A trigger logic will query another custom object which has got around 0.15 million records(150000). The query will have filters on 2 text fields.

 

Now if lets say we load data in first custom object using data loader which contains around 10,000 records. Trigger will fire in chunks of 200. And it will query second object with some filters and then populate first custom obect with data from second.

 

I just want to understand what would be best approach here, Keeping in mind the no of records that are present in the second object.

 

1. Keep the logic in Trigger

2. Move that whole logic inside a batch class.

 

Please provide your inputs.

 

Alok_NagarroAlok_Nagarro

Hi Shushil,

 

As per as your requirement, i think you will need to have both trigger as well as batch class. Since in trigger records are processed with batch of 200 recs so doesn't matter how much records you are uploading, Now problem is with second object's record need to be updated(can be many records). So as my understanding,solution is -

 

I. create trigger on fist object  - call the batch class and pass list of all id to batch class

II. create batch class - that will querying 2nd object's records and will be processing in small batches

   - in start method - query 2nd obj's records based on ids(got from trigger)

  - in execute method - process records as you want

 

Note - however you can use only batch class,in that case you need to schedule that class ,so it will execute periodacally.

sfdcsushilsfdcsushil
Hi Alok, Thanks for ur detailed reply. I think you took it little wrong. Mistake on my part in explaining the issue. Actually I have to query second object and iterate those recorda to find set of values. Then i have to assign those values to first object records. so my issue is querying second object in first object trigger. and second object has got about .15 million recs. That is why i aaked if it is better to write a batch with scope on first object. Please let me know ur thoughts. Thanks.
Alok_NagarroAlok_Nagarro

yes thats what i was saying that second object has got about 1.5 million records that can not be proccessed in trigger, so you need to put that logic in batch. Second thing in batch you will also need to have first object records to update (right ?).So just pass those record ids in batch from trigger.

 

Now in batch you have first object records and can query second object records as well and can process you logic here.