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
Jina ChetiaJina Chetia 

Need help in designing a batch process



I have to prepare two financial reports on nightly basis based on the every day feed that I get into salesforce. Here are the details -


1. There are two objects in salesforce Purchase Report and Portfolio Report and they are linked to the Account object. Account is the master for the two objects


2. Every day I get a feed of the Purchase Report which details out the money invested, date etc. Each record here requires some net gain % calculation which I am currently handling it thru a batch process.


3. Now based on the Purchase Report, I have to prepare  the Portfolio Report records which is the summary of the Purchse Report records..

  For e.g. - Account A has 100 records in the Purchase Report,out which there are 10 unique products..i.e. 10 records each for each product.

  So my summary report should generate 10 records, summing the total amount spent for 10 records and then other calculations like net gain % etc. Also, if there is already a record for the products in Portfolio Report object, it should just update the record instead of creating a new record. This will be something like the mutual find summary report and this update should run on nightly basis.


4. The daily feed that I would get may have around 50 K records.


I am confused on what approach should I take. If I use batch process, one batch will contain only 200 records and that might not include all the records for each product. I can store the summation on a stateful class variable but then how do I handle the after processing of data.

Should I do all my post calculations of calculating gain based on the summation in the 'finsh' method and upsert the records there? Is this a good a approach or there is some other way of doing it?


Many Thanks,



Would it be feasible to load the data first, and then batch link/attach/modify the records in the 200 record batches, and at the end of the process, do whatever you need to do for the set as a whole?  For example, do your batch data load, use a rollup summary field instead of Apex to calculate the summary data, and use your batch apex process, or a formula field, to populate the summary data to the other object.  You'd need a rollup summary field for each product's data, and subsequent fields in your initial data load, but it'd take the complexity out of the batch apex code.

Jina ChetiaJina Chetia

Hi Paul,


Thanks for the response. I would be loading the detail records for Purchase Report first based on which I will generate the summary report records.

But I am not quite sure on how to use the roll up summary field as the products are dynamic. Today there are 90 products say, tomorrow there can be 1000 products since these products are nothing but equities/stock. So there is control in my hands on the number of products, so the number of roll up summary field on my master object will also vary.


I hope I am clear. Please let me know if you need any further details.


ok, yeah, rollups wouldn't work for that then.  this is a bit bigger than I can really analyze and make a proper recommendation at the moment.  hopefully someone else on here can chime in with some assistance in the mean time.