function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

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){
        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);

            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);

User-added image
Sunil RathoreSunil Rathore
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:
system.debug('assocEvent.WhatId value '+ assocEvent.WhatId);
system.debug('assocState.get(assocEvent.WhatId) value '+ assocState.get(assocEvent.WhatId));
this debugs can help you to compare the exact values.

let me know if it helps you.

Sunil Rathore

Hi Sunil - Thanks for the response. When I change containsAll(stateSet) to contains(stateSet), I receive the below error because I'm comparing sets. Is there another option? Regarding the assocState map, in the debug log pic, the 3rd line is the map debug statement. You'll notice it shows the id and the correct state. Any thoughts as to why that would result as false? 

Method does not exist or incorrect signature: void contains(Set<String>) from the type Set<String>

Thanks again,
Sunil RathoreSunil Rathore
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 :  
Boolean statezSetcompare = stateSet.containsAll(gwStates);
For  Boolean state = gwStates.contains(assocState.get(assocEvent.WhatId));
is a 15 digit ID and the assocState map contains 18 digit IDthat is why it is returning you false.
try this:
ID assocEventID = assocEvent.WhatId;
Boolean state = gwStates.contains(assocState.get(assocEventID));

Let me know if it helps you out.

Many Thanks,
Sunil Rathore
Thanks again for this help. Can you please see below?
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. 
User-added image

here's the debug log with the changes I made from above. 
User-added image
Sunil RathoreSunil Rathore

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