Map sObjectsToClone = Map of child object along with there relationship name.

I have to clone each relationship to each opportunity.

 global List<sObject>  start(Database.BatchableContext BC) {
    List<Opportunity> OpportunityList = new  List<Opportunity>();
    List<Bulk_RAC__c> bulkRac = Database.query(originalObjectQuery);
    for(Account acc : [Select Id, Name, Org_ID__c from Account where Org_ID__c in :selectedAccIdList] ) {
        //** For each Account create multiple(multiplier) Opportunities
        String name = 'Bulk Oppy - '+ acc.name;
        while(counter < multiplier) { 
            Opportunity cloneOppy = orignalOppy.clone(false,true,false,false);
            cloneOppy.name = name;
            cloneOppy.AccountId = acc.Id;
        counter = 0;
    return OpportunityList;

//** execute method
global void execute(Database.BatchableContext BC, List<Opportunity> batch) {
    List<Opportunity> OpportunityList = new  List<Opportunity>();
    for(Opportunity opp : batch) {
    insert OpportunityList;

    //** Insert child records to each Oppy
    for(sObject sob : sObjectsToClone.keySet()) {
        List<sObject> childObjectStore = new List<sObject>();
        for(Opportunity sOppy : OpportunityList) {
            sObject clone = sob.clone(false, true);
            clone.put(sObjectsToClone.get(sob), sOppy.Id);
            system.debug('Clone :: '+clone);
        insert childObjectStore;
    //system.debug('childObjectStore :: '+ childObjectStore);


This code is work fine if OpportunityList size is small(say 10) but if OpportunityList size is more(say more than 100) it will throw System.LimitException: Apex heap size too large: exception.

Where sObjectsToClone is list of children object of original opportunity which I am passing through Wrapper class.

I need to clone some children (not all) and Original opportunity.

Please suggest any solution.

  • 1
    When is OpportunityList large? Perhaps this needs to become a batch apex job.
    – Phil Rymek
    Commented Feb 26, 2014 at 20:28
  • 4
    Your deep clone is inside two levels of loop. So if the outer loop is over 100 objects and so is the inner loop then that is 10,000 SObjects going into memory so not surprising you are exceeding the heap limit. If you describe what you are trying to do overall, perhaps someone can suggest an alternate algorithm before you resort to wrapping what may be an inappropriate algorithm in a batch job.. Note it is unusual to use the SObject as a map key; it is more common to use the SObject's Id.
    – Keith C
    Commented Feb 26, 2014 at 22:02
  • 1
    You need a lot to get over the heap size limit. Simply adding 100 opps to a child object store shouldn't be causing it. I'm thinking something upstream is causing the heap size to become really large and the opportunities are just the straw the broke the camel's back. Is there additional code that can be posted? Especially this sObjectsToClone variable and how it's populated?
    – willard
    Commented Feb 26, 2014 at 22:03
  • Maybe you can use Batch Apex? salesforce.com/us/developer/docs/apexcode/Content/… Commented Feb 27, 2014 at 8:00

1 Answer 1


I would suggest you to use smaller batches which don't fail.

Instead of

Database.executeBatch(new YourOpportunityBatchClassName, 1000);


Database.executeBatch(new YourOpportunityBatchClassName, 100);

or even

Database.executeBatch(new YourOpportunityBatchClassName, 50);

It will clone your child objects at the end of the day.

You must log in to answer this question.

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