The use case is sending an attachment(csv) to my email service ; in the email service i will parse the csv ; each line of the csv will have values like


I have to query the custom object based on the keys like :

Select Id,value1 from Object__c where KEY1 =:key1 and KEY2 =: key2;

and then update the value of the object. The attachment will contain around 10,000 lines , so i would have to do like 10K queries. In that case i would run into the governor limits , so is there a better way to do this ? Is Batch Apex job the direction to go ? Any links or suggestions is greatly appreciated.


1 Answer 1


Batch Apex is what I would use. This article Importing large CSV files via Batch Apex demonstrates how to deliver chunks of CSV file lines as the batch. So as well as SOQL governor limits, the batching helps with CPU time and heap governor limits.

You could try a batch size of 100, presuming there is no way to optimise the query. But if you have an update trigger on Object__c that does SOQL you might have to reduce this (or if the trigger is not needed for this update add a guard to it so that it does not run when this code is executing). All the found objects could be updated at once using a single update statement.

  • 1
    Yep, I've used that pattern myself - one thing to note is that it presumes your logic is such that each CSV row is independent of each other when it comes to updating. If x consecutive CSV rows update a single SObject, then the batch could be split in the middle of the set of x rows - creating logic complexities.
    – cropredy
    Commented Mar 14, 2014 at 0:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .