You need to sign in to do that
Don't have an account?
Chris Voge 9
Set contains duplicate elements
Hello,
We have an issue where a Set object contains duplicate elements,
but the elements contain the same hashCode. Isn't the hashCode the
determining factor when considering whether there are duplicate items
in a Set ?
The problem starts on line:
o.Most_Recent_Lead__c = lead.Id; (see below)
If I comment this line out , there is no issue.
We are then moving the Set object to a List object, expecting
it to have unique elements, then attempt to perform a Database.update.
When attempting this, we get
Duplicate id in list: 0065B000002HyxqQAC
Wondering why this is happening and if anyone can offer suggestions.
Much appreciated
Chris
We have an issue where a Set object contains duplicate elements,
but the elements contain the same hashCode. Isn't the hashCode the
determining factor when considering whether there are duplicate items
in a Set ?
The problem starts on line:
o.Most_Recent_Lead__c = lead.Id; (see below)
If I comment this line out , there is no issue.
We are then moving the Set object to a List object, expecting
it to have unique elements, then attempt to perform a Database.update.
When attempting this, we get
Duplicate id in list: 0065B000002HyxqQAC
for ( Opportunity o : oppList ){ if ( o.accountId != null ){ Id contactId = someMap.get( o.AccountId ); if ( contactId != null ){ List<Leads__c> tmpLeads = someOtherMap.get( contactId ); if ( tmpLeads != null ){ DateTime mostRecent; Leads__c mostRecentLead; for ( Leads__c lead : tmpLeads ){ lead.Opportunity__c = o.Id; if ( mostRecent == null || lead.createdDate > mostRecent ){ mostRecent = lead.createdDate; o.Most_Recent_Lead__c = lead.Id; } if(updateOppSet.contains(o)){ System.debug('Already contains opp'); } else { updateOppSet.add( o ); } System.debug('Hashcode: ' + updateOppSet.hashCode()); // Debugging purposes for ( Opportunity o1 : updateOppSet){ System.debug('Hashcode for elm: ' + System.hashCode(o1)); } } } } } } List<Opportunity> updateOppList = new List<Opportunity>(); updateOppList.addAll( updateOppSet ); Database.Saveresult[] results = Database.update(updateOppList, false);
Wondering why this is happening and if anyone can offer suggestions.
Much appreciated
Chris
Please try below code and let us know if that will help you
All Answers
Please try below code and let us know if that will help you
This should work well.
Thanks for the help all.
Chris