I am confused by my test results:
Set<opportunity> setOpp = new Set<opportunity>();
Opportunity opp = [select Id,CloseMonthText__c from Opportunity where id='0067000000hqu5X'];
for(integer i=0;i<10;i++){
opp.CloseMonthText__c='A'+i;
setOpp.add(opp);
}
System.debug('SetOpp: '+setOpp);
System.debug('size: '+setOpp.size());
For(opportunity o:setOpp){
System.debug(o);
}
The output I get is something like this:
SetOpp: {Opportunity:{Id=XXXX,CloseMonthText__c=A9}}
Size: 10
Opportunity:{Id=XXXX,CloseMonthText__c=A9
Opportunity:{Id=XXXX,CloseMonthText__c=A9
Opportunity:{Id=XXXX,CloseMonthText__c=A9
Opportunity:{Id=XXXX,CloseMonthText__c=A9
Opportunity:{Id=XXXX,CloseMonthText__c=A9
Opportunity:{Id=XXXX,CloseMonthText__c=A9
Opportunity:{Id=XXXX,CloseMonthText__c=A9
Opportunity:{Id=XXXX,CloseMonthText__c=A9
Opportunity:{Id=XXXX,CloseMonthText__c=A9
Opportunity:{Id=XXXX,CloseMonthText__c=A9
I know that set detect duplication of SObject by checking all the fields of the record. I think Set stores a reference to the actual instance. In the for loop it's also changing the referenced value of set. How come the set size is 10? Can anyone explain the output?
CloseMonthText__c
. Executed code in DevConsole anonymous window and could not reproduce. I got 3 debug outputs as one would expect.