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
J&A-DevJ&A-Dev 

Need help designing a batch process

Hi all,

 

I have to implement a batch process and I was thinking about building the application on the Force.com platform. I'd like to hear what your suggestions are, as I'm not sure if this is something I can do using Apex classes and triggers.

 

The goal is to provide sales summary data at the Account (object) level. The sales data is stored under contact records. Here's an example to help illustrate what I'm trying to achieve:

- Account "A" has 4 contacts with sales figures. The aggregated sales data (from all contacts belonging to Account A) will be written at the Account level.

-Ideally, this sales data present at the Account object should update when: a) the sales figure on any of its contact records change, or b) A new contat record is associated with Account A and has a sales figure > 0.

 

I wanted to build a trigger on the Contact object that will fire after inserts and after updates when the criteria is met. This should work for most users when creating/updating single records, but we also have a nightly job that updates the sales figures on a large number of contact records. This number varies from 500 to over 30K contact records. What would you suggest that I do? I know that Apex allows to retrieve up to 10K records from a single SOQL query.

 

Thanks in advance.

Best Answer chosen by Admin (Salesforce Developers) 
RajanJasujaRajanJasuja

Hi,

 

Rollup summery fields are only applicable with the master relationship, Contact have a lookup relationship with Account.

So we can’t create a contact rollup field on account. The only possible way is trigger.

 

Now as you mentioned, they you run a nightly utility which is updates the sales figures on a large number of contact records. If you are using a custom utility (Created using .Net or Java using Partner or Enterprise WSDL) then records are getting updated using Salesforce API calls.

The maximum number of records updated by an API call is 200, so you can maximum update 200 contacts in one API update call. When the trigger is fired for 200 contacts, Trigger.new and Trigger.old will contain 200 contact records and 200 contacts maximum can be associated with 200 Accounts.

So I don’t think you will face any issue regarding apex governor limit. Because select and update for 200 account on the basis of 200 contacts in Trigger.new will not throw any governor limit exception.

You have to tack care of the following

            Trigger key point  

  1. The triggers on Contact update and insert must be bulk safe.
  2. Must have “if else” condition for Trigger.new and Trigger.old sales fields.

 

Utility Key point

  1. If you are using SalesForce API then limit the batch size with 200 records per API call,  but if they are using any other way to update contacts then, where you can set the batch size more then 200 then set the batch size to 999 records per call.    

 

Hope this will help you.

 

Regards,

Rajan Jasuja

All Answers

SatgurSatgur

Hi,

 

Did you explore the option of using "Roll up summary" fields for this requirement? This is an in-built feature and should satisfy all the needs you stated.

 

Do let us now.

 

Also it would help to know what are the fields (from associated CONTACT records) that we need to aggregate on ACCOUNT.

 

 

Regards

Satgur

J&A-DevJ&A-Dev

Hi Satgur,

 

Yes, I did check the "Roll up Summary" field option, but I don't see a relationship (from the Account object) with the Contact object.

 

And the fields that I need to aggregate are custom currency fields stored in contact records.

RajanJasujaRajanJasuja

Hi,

 

Rollup summery fields are only applicable with the master relationship, Contact have a lookup relationship with Account.

So we can’t create a contact rollup field on account. The only possible way is trigger.

 

Now as you mentioned, they you run a nightly utility which is updates the sales figures on a large number of contact records. If you are using a custom utility (Created using .Net or Java using Partner or Enterprise WSDL) then records are getting updated using Salesforce API calls.

The maximum number of records updated by an API call is 200, so you can maximum update 200 contacts in one API update call. When the trigger is fired for 200 contacts, Trigger.new and Trigger.old will contain 200 contact records and 200 contacts maximum can be associated with 200 Accounts.

So I don’t think you will face any issue regarding apex governor limit. Because select and update for 200 account on the basis of 200 contacts in Trigger.new will not throw any governor limit exception.

You have to tack care of the following

            Trigger key point  

  1. The triggers on Contact update and insert must be bulk safe.
  2. Must have “if else” condition for Trigger.new and Trigger.old sales fields.

 

Utility Key point

  1. If you are using SalesForce API then limit the batch size with 200 records per API call,  but if they are using any other way to update contacts then, where you can set the batch size more then 200 then set the batch size to 999 records per call.    

 

Hope this will help you.

 

Regards,

Rajan Jasuja

This was selected as the best answer
J&A-DevJ&A-Dev

Hi Rajan,

 

I read about the 200 records / update call rule but wanted to double check I wasn't missing anything before I start coding :smileyhappy: Your response was very helpful. Thank you.

 

So I take it that if we were to load contact records via the data loader, we should expect the same results? For example, say we load 10,000 contact records that will fire my trigger. If the batch size on data loader is set to 200 records, then we could just submit the entire 10,000 records and the app should package the updates in batches of 200 records/call, correct? 

RajanJasujaRajanJasuja

Yes, Data loader follows the same approach to break the records in 200 batch size per API call.So hopefully you will not face any issue with your trigger while lode 10,000 records using data loader.Just remind your trigger code must be bulk safe. 

 

Cheers! Rajan

 

   

Message Edited by RajanJasuja on 07-15-2009 08:38 AM
J&A-DevJ&A-Dev
Absolutely. I'll make sure that the trigger is bulk safe. Thanks for your help!