I am trying to upsert some CustomObject__c record based on some criteria related to Opportunity Records.
Set<Property2__c> properties = new Set<Property2__c>();
List<Property2__c> upsertProperties = new List<Property2__c>();
for (Opportunity opp : opps) {
Property2__c prop = new Property2__c();
String propCheck = opp.Street_opp__c + '---' + opp.Postal_Code_ZIP_opp__c;
if (szToProperty.containsKey(propCheck)) {
prop = szToProperty.get(propCheck); // check for property
properties.add(prop);
}
else{
if (!properties.contains(prop)) properties.add(prop);
prop.Name = opp.Street_opp__c;
prop.Account__c = opp.AccountId;
prop.Street__c = opp.Street_opp__c;
}
if (!properties.contains(prop)) {
properties.add(prop);
} // ensure we don't update twice. -- upsertProperties.add(prop);
}
}
if (properties.size() > 0) {
upsertProperties.addAll(properties);
System.debug('\n\n---Upsert Size---: ' +upsertProperties.size());
upsert upsertProperties;
}
But I am getting some Exception -
System.ListException: Before Insert or Upsert list must not have two identically equal elements
I am checking for the Duplicate Record in this code but still getting this exception. Any Help??