1

I have this issue. I have a list of Contracts and I need to create Cases and custom objects called 'Budget' that the Case needs to lookup.

The code can be summarized to something like this

for(Contract c : contractList) {
   if(c.type = 'budget') {
      Budget b = new budget();
      Case cs = new Case();
      cs.type = 'budget case';
      cs.budget__c = b.id;
   } else {
      Case cs = new Case();
      cs.type = 'other case';
   }
}

Notice immediately the issue in which Budget needs to be set to Case, but Budget is still not yet inserted so the line, cs.budget__c = b.id will be null.

So in order to avoid adding an insert inside the loop for Budget, I have to put them into a list.

List<Case> allCaseList = new List<Case>();
List<Budget> allBudgetList = new List<Budget>();
for(Contract c : contractList) {
   if(c.type = 'budget') {
      Budget b = new budget();
      Case cs = new Case();
      cs.type = 'budget case';
      cs.budget__c = b.id;

      allBudgetList.add(b);
      allCaseList.add(cs);
   } else {
      Case cs = new Case();
      cs.type = 'other case';
      allCaseList.add(cs);
   }
}
insert allBudgetList;
insert allCaseList;

Now I have another dilemma. How can I pair back the Case with its corresponding Budget? Since we're no longer inside the loop, how can we set the correct Budget object as the lookup for Case?

I was thinking of perhaps making a map to pair both Case and Budget but without any other indicators to tie these two objects other than the Budget lookup in Case, how can we do that?

Thanks.

2

You can create a separate list to keep track of the cases associated to the budgets. This works because of the fact that objects are stored by "reference", meaning that an update to one object in memory affects all its references. Here's how you can do that:

List<Case> allCaseList = new List<Case>();
List<Case> budgetCaseList = new List<Case>();
List<Budget> allBudgetList = new List<Budget>();
for(Contract c : contractList) {
   if(c.type == 'budget') {
      Budget b = new budget();
      Case cs = new Case();
      cs.type = 'budget case';
      allBudgetList.add(b);
      allCaseList.add(cs);
      budgetCaseList.add(cs);
   } else {
      Case cs = new Case();
      cs.type = 'other case';
      allCaseList.add(cs);
   }
}
insert allBudgetList;
for(Integer i = 0, s = budgetCaseList.size(); i < s; i++) {
    budgetCaseList[i].Budget__c = allBudgetList[i].Id;
}
insert allCaseList;

Notice how we add the same case (cs) to both lists. By updating the values in budgetCaseList, we also automatically update the value in allCaseList. The net result is that our cases will be correctly linked to the budget that was created for each case.

Note that we don't need any other "reference"; we're using the fact that objects in memory are referenced by memory address, thus allowing us to update one list by updating the values in another list.

  • This looks really good! Thank you for the explanation and it all makes sense. I'm going to apply this to my code and I'll get back to you after testing it. Thanks again! – Jerard Dela Victoria Nov 12 at 6:26
  • So we tested our code and it seems we're no longer encountering any issues so I guess everything is all good now. Again, thanks so much for this help! – Jerard Dela Victoria Nov 14 at 4:08

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.