You need to sign in to do that
Don't have an account?

containsAll() from a Set returning false
Hello all - I'm trying to use containsAll() but it is returning false (bullet point 3). Below is a pic of the debug log which shows the states from the set. Any help would be greatly apprceciated.
1. I'm retrieving field values from a text area field called 'States' on a custom setting object and adding the states to a Set. (the custom settings is a list)
//Get states from custom settings field and add to set
Set<String> gwStates = new Set<String>();
for(GW_States__c gwSts : GW_States__c.getAll().values()){
if(gwSts.States__c != null){
gwStates.add(gwSts.States__c);
}
}
System.debug('States in Set ' +gwStates);
2. Then I query a custom object called Association and add the Association's state to a set and a map.
//map/set of states from association
Map<Id, String> assocState = new Map<Id, String>();
Set<String> stateSet = new Set<String>();
if(assoc.size() > 0 ){
for(Association__c associt : assoc){
assocState.put(associt.Id, associt.Association_State_Province__c);
stateSet.add(associt.Association_State_Province__c);
}
}
System.debug('State in set from association ' + stateSet);
System.debug('State in map ' +assocState);
3. Now I'm checking to see if the Association state is contained in the gwStates set and the map. Both return false. (see debug log)
//Use compare method to check for matching states
Boolean state = gwStates.contains(assocState.get(assocEvent.WhatId));
Boolean statezSetcompare = gwStates.containsAll(stateSet);
system.debug('state compare from map = ' +state);
system.debug('state compare from Assoc set = ' +statezSetcompare);

1. I'm retrieving field values from a text area field called 'States' on a custom setting object and adding the states to a Set. (the custom settings is a list)
//Get states from custom settings field and add to set
Set<String> gwStates = new Set<String>();
for(GW_States__c gwSts : GW_States__c.getAll().values()){
if(gwSts.States__c != null){
gwStates.add(gwSts.States__c);
}
}
System.debug('States in Set ' +gwStates);
2. Then I query a custom object called Association and add the Association's state to a set and a map.
//map/set of states from association
Map<Id, String> assocState = new Map<Id, String>();
Set<String> stateSet = new Set<String>();
if(assoc.size() > 0 ){
for(Association__c associt : assoc){
assocState.put(associt.Id, associt.Association_State_Province__c);
stateSet.add(associt.Association_State_Province__c);
}
}
System.debug('State in set from association ' + stateSet);
System.debug('State in map ' +assocState);
3. Now I'm checking to see if the Association state is contained in the gwStates set and the map. Both return false. (see debug log)
//Use compare method to check for matching states
Boolean state = gwStates.contains(assocState.get(assocEvent.WhatId));
Boolean statezSetcompare = gwStates.containsAll(stateSet);
system.debug('state compare from map = ' +state);
system.debug('state compare from Assoc set = ' +statezSetcompare);
containsAll() returns true if the set contains all of the elements in the specified set. The specified set must be of the same type as the original set that calls the method.
Here your set gwStates contains two values while stateSet contains only one value that is why it is returning false.
Instead of Boolean statezSetcompare = gwStates.containsAll(stateSet);
you can use Boolean statezSetcompare = gwStates.contains(stateSet);
However, for this Boolean state = gwStates.contains(assocState.get(assocEvent.WhatId));
you can use following debugs which can help you: this debugs can help you to compare the exact values.
let me know if it helps you.
Thanks,
Sunil Rathore
Method does not exist or incorrect signature: void contains(Set<String>) from the type Set<String>
Thanks again,
As stated in the above comment,
ContainsAll() returns true if the set contains all of the elements in the specified set. The specified set must be of the same type as the original set that calls the method.
Here your set gwStates contains two values while stateSet contains only one value that is why it is returning false.
To compare both tese sets you can use it oppositely like : For Boolean state = gwStates.contains(assocState.get(assocEvent.WhatId));
assocEvent.WhatId is a 15 digit ID and the assocState map contains 18 digit IDthat is why it is returning you false.
try this:
Let me know if it helps you out.
Many Thanks,
Sunil Rathore
I'm now passing in the 18 digit Id but it still shows false (see row 4 and 5 in the debug log)...
ID assocEventID = assocEvent.WhatId;
System.Debug('ID assocEventId = ' +assocEventID);
Boolean state = gwStates.contains(assocState.get(assocEventID));
and changed the contains method to this...
Boolean statezSetcompare = stateSet.containsAll(gwStates);
Unfortunately, they are still returning false. Could it be something to do with the custom setting field? Below is a pic of the CS field.
here's the debug log with the changes I made from above.
Got the point, your custom setting field contains IN,CO which is treating as a single value in a set that is why it is returning false.
Let's keep only 'IN' in the custom setting field and run your code. This time it will return true.
Let me know if it solves your problem.
Many Thanks,
Sunil Rathore