1

While using system.debug for a list I get an already output statement added in the logs as

USER_DEBUG|[]|DEBUG|lstApprovalDetails (ApprovalDetails:[startDate=2002-01-03, status=Rejected], (already output))

CAppresponse.ApprovalDetails AppDet = new CAppresponse.ApprovalDetails();

for(Approval_RD__c appDetail:lstRD)
{
    AppDet.startDate=string.valueof(appDetail.Start_Date__c);
    AppDet.status=appDetail.Status__c;

    lstApprovalDetails.add(AppDet);

    system.debug('lstApprovalDetails '+ lstApprovalDetails);    
}

Wanted to know the meaning of this already output here .Thanks in advance

1

1 Answer 1

1

It means you have multiple keys/indices that each have the same logical object (same memory address/reference). This usually indicates a logic error. If you see this message, your code may be recycling variable names inappropriately instead of initializing new objects. There are times when this is okay, but you should be aware that when this happens, you can end up with unexpected effects.

Consider the following code:

List<List<Integer>> values = new List<List<Integer>>();
List<Integer> value = new List<Integer>();
value.add(42);
values.add(value);
value.add(24);
values.add(value);
System.debug(values);

If you expected the output to be:

((42), (24))

This would be incorrect. Because we forgot to initialize value, it causes:

((42, 24), (already output))

To fix this output, we need to make a new list:

List<List<Integer>> values = new List<List<Integer>>();
List<Integer> value = new List<Integer>();
value.add(42);
values.add(value);
value = new List<Integer>();
value.add(24);
values.add(value);
System.debug(values);

((42), (24))

While this trivially obvious, usually, such a logic bug will be done in a loop (as in your example code). For example:

// Usually incorrect
List<List<Integer>> values = new List<List<Integer>>();
List<Integer> value = new List<Integer>();
for(Integer i = 0; i < 2; i++) {
  value.add(i);
  values.add(value);
}

// Fixed
List<List<Integer>> values = new List<List<Integer>>();
for(Integer i = 0; i < 2; i++) {
  List<Integer> value = new List<Integer>();
  value.add(i);
  values.add(value);
}

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