You need to sign in to do that
Don't have an account?
Tony White (BNE)
Comparison method override fails in UAT environment
I have a bit of a weird problem... the code below works correctly in our development environment, but when we load it into our UAT environment it does not work for 2 out of 3 people. All 3 people have the same profile (sys admin), we have tried different browsers, using Eclipse but it only works for the 1 user.
Any suggestions as to what is going on?
Thanks
Tony
public class getDuplicateAffiliations {
public class memberLodge {
id member;
id lodge;
public memberLodge(id m, id l){
member = m;
lodge = l;
}
public Boolean equals(Object obj) {
system.debug('CompareA:'+member+','+lodge);
system.debug('Compareb:'+((memberLodge)obj).member+','+((memberLodge)obj).lodge);
return (member == ((memberLodge)obj).member && lodge == ((memberLodge)obj).lodge);
}
}
public static List<npe5__Affiliation__c> getDuplicates() {
List<position__c> positionList = [select id, end_date__c, Member_Lodge_Affiliation__r.npe5__Contact__c from position__c
where end_date__c = null and Member_Lodge_Affiliation__r.npe5__EndDate__c != null];
List<id> memberList = new List<id>();
for (position__c p : positionList) {
memberList.add(p.Member_Lodge_Affiliation__r.npe5__Contact__c);
}
System.debug(positionList);
System.debug(memberList);
map<memberLodge, npe5__Affiliation__c> affiliations = new map<memberLodge, npe5__Affiliation__c>();
List<npe5__Affiliation__c> duplicateAffil = new List<npe5__Affiliation__c>();
for (npe5__Affiliation__c aff : [select id, npe5__Contact__c, npe5__Organization__c, Lodge_Masonic_Status__c
from npe5__Affiliation__c
where npe5__Contact__c in :memberList]) {
memberLodge key = new memberLodge(aff.npe5__Contact__c, aff.npe5__Organization__c);
System.debug('aff:'+aff);
System.debug('mlkey:'+key);
if(affiliations.containsKey(key)) {
duplicateAffil.add(aff);
duplicateAffil.add(affiliations.get(key));
}
else {
affiliations.put(key,aff);
}
}
System.debug(duplicateAffil);
return duplicateAffil;
}
}
Any suggestions as to what is going on?
Thanks
Tony
public class getDuplicateAffiliations {
public class memberLodge {
id member;
id lodge;
public memberLodge(id m, id l){
member = m;
lodge = l;
}
public Boolean equals(Object obj) {
system.debug('CompareA:'+member+','+lodge);
system.debug('Compareb:'+((memberLodge)obj).member+','+((memberLodge)obj).lodge);
return (member == ((memberLodge)obj).member && lodge == ((memberLodge)obj).lodge);
}
}
public static List<npe5__Affiliation__c> getDuplicates() {
List<position__c> positionList = [select id, end_date__c, Member_Lodge_Affiliation__r.npe5__Contact__c from position__c
where end_date__c = null and Member_Lodge_Affiliation__r.npe5__EndDate__c != null];
List<id> memberList = new List<id>();
for (position__c p : positionList) {
memberList.add(p.Member_Lodge_Affiliation__r.npe5__Contact__c);
}
System.debug(positionList);
System.debug(memberList);
map<memberLodge, npe5__Affiliation__c> affiliations = new map<memberLodge, npe5__Affiliation__c>();
List<npe5__Affiliation__c> duplicateAffil = new List<npe5__Affiliation__c>();
for (npe5__Affiliation__c aff : [select id, npe5__Contact__c, npe5__Organization__c, Lodge_Masonic_Status__c
from npe5__Affiliation__c
where npe5__Contact__c in :memberList]) {
memberLodge key = new memberLodge(aff.npe5__Contact__c, aff.npe5__Organization__c);
System.debug('aff:'+aff);
System.debug('mlkey:'+key);
if(affiliations.containsKey(key)) {
duplicateAffil.add(aff);
duplicateAffil.add(affiliations.get(key));
}
else {
affiliations.put(key,aff);
}
}
System.debug(duplicateAffil);
return duplicateAffil;
}
}
In Dev the CompareA and CompareB debug lines are printed out, but not in UAT. Making me believe that the code is not using that equals method.
The test class creates the test data itself and does not rely on any existing data.
If I add this code in before the affiliations.containsKey test, it will successfully use the overridden equals method and output the debug and the test passes as it correctly matches a duplicate.
if (affiliations.size() > 0) {
for (MemberLodge akey : affiliations.keySet()) {
if (akey.equals(key)) {
system.debug('Match found:'+akey+':'+key);
break;
}
}
}
But as soon as I comment out that code, it no longer uses the overridden equals method as it no longer logs the debug from there...
Cheers
Tony
I think your issue here is that the ContainsKey is looking for the same instance of an object, and the object you're passing in a new MemberLodge every time therefore its failing..
where as with the loop solution you're checking if the two Instances of the Key (memberLodge) are of equal value hence that is working.
although this does seem like a bug is SFDC it kind of makes sense when i think about it from a dev pespective
hope this makes snese.
1) containsKey should be using the object's own equals method - https://www.salesforce.com/us/developer/docs/apexcode/Content/langCon_apex_collections_maps_keys_userdefined.htm
2) That does not explain why it works for 1 user and not the others, because that is another aspect we are seeing.
T.