+ Start a Discussion
Aslam iqbal - SoqlAslam iqbal - Soql 

Batch class / job best practices

I am looking for an advice on batch jobs best practices.

1. We have a subscription custom object that has a lookup to contact.
2. We currently have a bach job 'nightly subscription update' that runs at 10pm to sets the status of subscription based on 'lastmodifieddate' (set status to lapsing, lapsed, and deleted),
3. There is a process builder on subscription, when changed calls flow to set the contact status (recalculates contact status based on related subscriptions).
4. there are few million subscriptions.

Now I have a 2 new requirements that
a. Delete subscriptions if today >subscription 'deletedate' and
b. Delete Contact if today > contact 's'deletedate'.

Solution that I am thinking of created 2 seperate batch classes a. 'subscription delete' and b. 'contact delete'.
And schedule/run each of this batch job without interfering one another.

i.e schedule as below
1. 'nightly subscription update' 10pm
2. 'nightly subscription Delete' 11pm
1. 'nightly contact delete' 12pm

with this approach, I am assuming the batch job will start at the exact time and finished i an hour.

Hence looking for help the better architect the solution 


 
Best Answer chosen by Aslam iqbal - Soql
Ramesh DepaiahRamesh Depaiah
Hi  Aslam,
Instead of scheduling subscription Delete and contact delete at 11 & 12 pm what you can do is schdeule subscription Delete once Subscription update is finished 
global void finish(Database.BatchableContext BC) {
    //Schedule delete subscription here  
}

Do the same for nightly contact delete, schedule it on finish of  subscription Delete


I hope you find the above solution helpful. If it does mark as best answer to help others too.

Thanks,
Ramesh D

 

All Answers

Ramesh DepaiahRamesh Depaiah
Hi  Aslam,
Instead of scheduling subscription Delete and contact delete at 11 & 12 pm what you can do is schdeule subscription Delete once Subscription update is finished 
global void finish(Database.BatchableContext BC) {
    //Schedule delete subscription here  
}

Do the same for nightly contact delete, schedule it on finish of  subscription Delete


I hope you find the above solution helpful. If it does mark as best answer to help others too.

Thanks,
Ramesh D

 
This was selected as the best answer
Aslam iqbal - SoqlAslam iqbal - Soql
thanks @ramesh
your solution looks right to me.
will wait to see if others have a view on this and then mark your solution as correc.

Adition to this is there best practive / recomendaton such as one batch class for each object?
(similar recomended way to one trigger for each object)


regards
Aslam
 
Ramesh DepaiahRamesh Depaiah
@aslam This article talks about Batch Apex Governor Limits & Best Practices in depth
https://salesforcean.blogspot.com/2017/10/batch-apex-governor-limits-best.html

Cheers
​​​​​​​Ramesh