I am getting list of opportunities and 1 master opportunity, using batch apex I am updating list of oppys from master oppy, what is the best way to know that child objects- say opportunityLineItems and attachments - are updated or not, if yes, the how can i update children?

For Example: I have an Opportunity with 2 OpportunityLineItems, 5 Activities and 10 Attachments. I want to clone this opportunity 200 times along with OpportunityLineItems and Attachments only using batch apex.

As per my understanding I have to create Batch Apex, In execute method I have to clone and insert the Opportunities first, then I have to clone Attachments and OpportunityLineItems for each of the Opportunity. But while doing so if I set the batch size to 20, it will throw the error of Heap Space.

Is there any efficient way to achieve this?

Thanks in advance.

  • 2
    Can you please add some more details to your question. Maybe an extract of your batch apex showing the core steps and where you are having an issue determining if OLIs "are updated or not". As a wild guess, you might want Database.Stateful – Daniel Ballinger Feb 27 '14 at 20:45
  • added more details. – Bharat Feb 28 '14 at 8:38

It is almost certainly the multiple attachments that you are cloning that are causing you to exceed the Heap Space limit. In a batch job you get the asynchronous limit, which is currently 12 MB.

You will need to start a secondary batch job to clone the attachments. Depending on the size of the attachments you may be best setting the batch size to 1, maybe 2.

During the batch you can check the current heap size using Limits.getHeapSize() and Limits.getLimitHeapSize(). If the current heap size plus the size of the attachment you want to load into memory exceeds the heap limit then you will get an exception. Worse still with cloning as you will probably end up with two copies of the attachment in the heap at any one time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.