0

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){
                    articleIds.add(cua.Article_ID__c);
                    for(articleWrapper wrpr : cuWrapperMap.values()){
                        //Here I want to iterate only for wrpr.articleId's that doesn't exist in the set.
                        if(!(wrpr.articleId).contains(articleIds)){

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

            }
insert createCUAList;

3
  • 1
    What part of your code doesn't do what you want it to?
    – Nick C
    Commented Sep 3, 2019 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
    Commented Sep 3, 2019 at 1:21
  • Hey Nick, if(!(wrpr.articleId).contains(articleIds)){ this doesn't work
    – Ravi Teja
    Commented Sep 3, 2019 at 4:25

1 Answer 1

0

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

contains(substring)

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

contains(setElement)

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

if(!(wrpr.articleId).contains(articleIds))

to

if(!articleIds.contains(wrpr.articleId))

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

2
  • 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
    Commented Sep 3, 2019 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. Commented Sep 4, 2019 at 2:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .