0

I have over 200 records, and I need to insert 2 lists. Work's records are created too fast and can't take a customField from still not inserted Case. If insert 1 per time - Error: Too many DML statements: 151.

My code:

public class Helper {

    public static void upd(Map<Id, Case> cases){

        List<Case> newCases = new List<Case>();
        List<Work__c> wp = new List<Work__c>();

        for(Case c1 : cases.values()){
            Case c2 = new Case(Origin = 'Web', Status = 'New');
            newCases.add(c2);

            for(Work__c w1 : [SELECT Id, Quantity__c, Custom_Field__c FROM Work__c WHERE Custom_Field__c =: c1.Id]){
                Work__c w2 = new Work__c (Quantity__c = w1.Quantity__c, Custom_Field__c = c2.Id);
                wp.add(w2);
            }  
        }
        insert newCases;
        insert wp;
    }
}
-2

public class Helper {

public static void upd(Map<Id, Case> cases){

    List<Case> newCases = new List<Case>();
    List<Work__c> wp = new List<Work__c>();

    for(Case c1 : cases.values()){
        Case c2 = new Case(Origin = 'Web', Status = 'New');
        newCases.add(c2);  
    }
    insert newCases;

    for(Work__c w1 : [SELECT Id, Quantity__c, Custom_Field__c FROM Work__c WHERE Custom_Field__c IN: cases.keyset()]){
        for(Case c2 : newCases){
            Work__c w2 = new Work__c (Quantity__c = w1.Quantity__c, Custom_Field__c = c2.Id);
            wp.add(w2);
        }

    } 
    insert wp;
}

}

  • 1
    wihtout any explanation, its kind of hard to see how this actually answers the OPs questions – glls Aug 1 '18 at 19:31
  • My bad, I am new to this forum. I focused on bulkifying the OP's code because the Error: Too many DML statements: 151 in the OP's code was a clear indicator of unbulkified code. – Steven Nsubuga Aug 1 '18 at 19:35
  • take a look at @David Reed answers, it provides an explanation of what doesnt seem to work. The answer not only helps the OP but any other person that has a similar if not the same problem. Providing copy paste snipets of code wont help other users resolve their problems and will lead to other users post the same quesiton over and over again with slightly different code. Keep in mind that this forum is not a Free coding service. – glls Aug 1 '18 at 19:39
  • This is bulkified incorrectly. The original code added a Work__c record to a new Case for each Work__c record on the original Case. Your code adds a copy of every Work__c record attached to any original Case to each new Case. – David Reed Aug 1 '18 at 19:42
  • Got it. I've gotten code answers here before so I simply wanted to return the favor. – Steven Nsubuga Aug 1 '18 at 19:43
5

Your Case.Id values are null until your perform the insert with insert newCases.

You should be performing two DML operations, as you do here, and only a single SOQL query. Right now, you're performing SOQL in a loop.

You need to refactor your logic to look something like this:

Iterate over Cases and accumulate Ids in a Set
Query against Cases by Id, with a subquery on Work__c
Iterate over queried Cases 
    For each Case, create a new Case.
Insert new Cases
Iterate over new Case list using an Integer index
    For each new Case, use the index in the list to get the corresponding old Case
    Iterate over the old Case Work__c items.
        For each old Case Work__c, create a new Work__c linked to the new Case
Insert new Work__cs

That will be fully bulkified and get you to the 2 DML, 1 SOQL point.

If you have an External Id field available on Case, you can reduce to a single DML operation by following the procedure in Creating Parent and Child Records in a Single Statement Using Foreign Keys. The logic above does not require External Ids.

-1

If you use SObject type, then you can insert all of them at once.

Example:

List<SObject> records = new List<SObject>();
Case c2 = new Case(Origin = 'Web', Status = 'New');
records.add(c2);
Work__c w2 = new Work__c (Quantity__c = 22, Custom_Field__c = 'some val');
records.add(w2);
Database.insert(records);

I strongly suggest following @David Reed's Logic to Bulkify your existing logic and these use this method to insert records of multiple objects at once.

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.