I'm iterating over below map. I want to compare map values and set and iterate over the map.values that doesn't exist in set

Set<String> articleIds = new Set<String>(); 

List<object__c> cuList = [SELECT Article_ID__c FROM object__c WHERE Article_ID__c IN: cuWrapperMap.keySet()];

            if((cuWrapperMap.keySet().size() > cuList.size()) && cuList.size() > 0){
                for(object__c cua : caList){
                    for(articleWrapper wrpr : cuWrapperMap.values()){
                        //Here I want to iterate only for wrpr.articleId's that doesn't exist in the set.

                            object__c cuaRec = new object__c();
                            cuaRec.Case__c = wrpr.caseId;
                            cuaRec.Article_ID__c = (wrpr.articleId).substring(0, 15);

insert createCUAList;

  • 1
    What part of your code doesn't do what you want it to? – Nick Cook Sep 3 '19 at 1:04
  • 1
    at the surface, this looks simpler to do by selecting into a Map and then pivoting the map based on article_id__c. see this answer – cropredy Sep 3 '19 at 1:21
  • Hey Nick, if(!(wrpr.articleId).contains(articleIds)){ this doesn't work – Ravi Teja Sep 3 '19 at 4:25

You are trying to use String class's contains method whereas you should be using Set class's contains method. And string's contains method does not take set as parameter, hence you might be receiving the compilation error.

String's contains method


Returns true if and only if the String that called the method contains the specified sequence of characters in substring.

String myString1 = 'abcde';
String myString2 = 'abcd';
Boolean result = myString1.contains(myString2);
System.assertEquals(result, true);

Set's contains method


Returns true if the set contains the specified element.

Set<String> myString = new Set<String>{'a', 'b'};
Boolean result = myString.contains('z');
System.assertEquals(false, result);

So change your below code




which will check if the wrpr.articleId is present in articleIds set or not

| improve this answer | |
  • Hi Vijay, if(!articleIds.contains(wrpr.articleId)) works for my logic to insert records. It's not working for my logic to delete records. I've update above code with my deleteArticles method. Can you pls check? – Ravi Teja Sep 3 '19 at 23:01
  • @RaviTeja your debug doesn't prove that whether the contains methods is working or not. Debug articleIds and wrpr.articleId to verify that. – Vijay Ganji Sep 4 '19 at 2:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.