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
Soumya sri ReddySoumya sri Reddy 

hi iam new to salesforce, code is working fine used contains method, to check duplicate

hi, my code is working, fine. to check duplicates used contains method , 
if  exists iam getting true value, but i need to get the false value also if not in list , how can  i solve this issue ..

User-added image

need to add : false ,for the 'b' as it wont exists
Apex class:
public class test3{
 public list<string>listSelectedNames{get;set;}
 public list<Wrapperclass>wraplist{get;set;}
 public list<boolean> Selectedbooleanlist{get;set;}
 public list<string>duplicatevalues{get;set;}
  
  public test3(){
 listSelectedNames = new list<string>();
 Selectedbooleanlist= new list<boolean>();
 duplicatevalues= new list<string>{'a','c'}; //duplicated to filter
 wraplist = new list<Wrapperclass>();
   Wrapperclass   wf = new Wrapperclass();
                  wf.selected=false;
                  wf.name ='a';
                 
    Wrapperclass   wf1 = new Wrapperclass();
                  wf1.selected=false;
                  wf1.name ='b';
                                  
   Wrapperclass   wf2 = new Wrapperclass();
                  wf2.selected=false;
                  wf2.name ='c';
                
          wraplist.add(wf);
          wraplist.add(wf1);
           wraplist.add(wf2);
        
 }
 
 
   public void duplicatecheck(){
   listSelectedNames.clear();
   Selectedbooleanlist.clear();
    for(wrapperclass wrap: wraplist){
      if(wrap.selected == true){
    listSelectedNames.add(wrap.name);
        }  
      }
     
     //to check duplicate
     for(String s1 : duplicatevalues){
      if(listSelectedNames.contains(s1)){
        boolean selected =  true;
        Selectedbooleanlist.add(selected);
         
   
      //add logic 
       }
    } 
        
        }
 //wrapper classs
    public class Wrapperclass{
       public boolean selected{get;set;}
       public string Name{get;set;}
    }
  
  
}


Visualforce Page:

 
<apex:page controller="test3">
   <apex:form >
   <apex:pageBlock >
      <apex:pageblockTable value="{!wraplist}"  var="a">
       <apex:column ><apex:inputCheckbox value="{!a.selected}"/>{!a.name}</apex:column>
       </apex:pageblockTable>
    <apex:commandButton value="OrderSelected"  reRender="one,two" action="{!duplicatecheck}"/>
      <apex:outputLabel id="one">{!listSelectedNames}</apex:outputLabel>
       <apex:outputLabel id="two">{!Selectedbooleanlist}</apex:outputLabel>
    </apex:pageBlock>
   </apex:form>
</apex:page>
//as it contains iam getting true, but to get false also if it dnt contains in the list of boolean
please help in this issue iam stucked
advance thanks
soumya yerava .
 
Nitin ShyamnaniNitin Shyamnani

Please update your function duplicateCheck code like this

 

public void duplicatecheck(){
    listSelectedNames.clear();
    Selectedbooleanlist.clear();
    for(wrapperclass wrap: wraplist){
        if(wrap.selected == true){
            listSelectedNames.add(wrap.name);
        }  
    }
    
    //to check duplicate
    for(String s1 : duplicatevalues){
        boolean selected =  false;
        if(listSelectedNames.contains(s1)){
        	selected =  true;    
        }
        Selectedbooleanlist.add(selected);
    } 
}


I checked using above code and it is working fine

Please let me know if you any other issue.

Soumya sri yaravaSoumya sri yarava
hi, nitin, firstly i would like to thank  u for ur helpful reply..
your code is Partially Working fine, 
but if we select a, b, c, at a time ... it should be [ true,false ,true]
if we select only a --> it should be only [ ture]
if we select only b--> it should be only [false]
if we select only c--> it should be only[ture] 
if we select b and c--> it should be false and ture --[false,true]
if we select a and c--> it should be false and ture --[true,true]
User-added image
Nitin ShyamnaniNitin Shyamnani
Please replace your function code with this
 
public void duplicatecheck(){
    listSelectedNames.clear();
    Selectedbooleanlist.clear();
    for(wrapperclass wrap: wraplist){
        if(wrap.selected == true){
			boolean selected =  false;
			if(duplicatevalues.contains(wrap.name)){
				selected =  true;
			}
			Selectedbooleanlist.add(selected);
        }  
    }
}

Please let me know in case of any issue.
Soumya sri yaravaSoumya sri yarava
Whooh..!!!  Amazing, @Nitin Shyamnani thanks for ur hands on help..  found the issue...
thank u so much
Soumya, 
 
Nitin ShyamnaniNitin Shyamnani
Please mark this solved and my answer as best, so it will help others too.

​​​​​
Soumya sri yaravaSoumya sri yarava
hi @nitin, iam trying to mark it as best answer, when it was not showing the Bestanswer popup if i keep mouse on LIKE symbol, before it was showing
 
Nitin ShyamnaniNitin Shyamnani
When you login with your account, it will show Mark as Best answer / Select as Best Answer option(option is available close to Like button)

We just need to resolved that question and mark an answer as Best