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
Harshal Katole 7Harshal Katole 7 

comparing two multiselectpicklist value

object = contact
multiselect picklist 1 = abc__c => values : 1,2,3,4,5,6,7
multiselect picklist 2 = xyz__c => values : 1,2,3,4,5,6,7
 scenario : check both field value not same while creating a record. if its same then thrown error.
 not working ?


public class checkdupcontact{
    public static void invoke(list<contact> cc){
        for(contact a : cc){
            list<string> VarList1 = a.abc__c.split(';'); 
            list<string> VarList2 = a.xyz__c.split(';'); 
            set<string> VarSetAdd1 = VarSetAdd1.addAll(VarList1);
            set<string> VarSetAdd2 = VarSetAdd2.addAll(VarList2);
            
            if(VarSetAdd1.containsAll(VarSetAdd2)){
                a.addError('error');
                
            }
        }
    }
}    
 
Maharajan CMaharajan C
Hi Harshal,

Are u getting any error? Based on my understanding you will get the runtime error due to the below lines:
set<string> VarSetAdd1 = VarSetAdd1.addAll(VarList1);
set<string> VarSetAdd2 = VarSetAdd2.addAll(VarList2);

Please update the your code as below:
 
public class checkdupcontact{
    public static void invoke(list<contact> cc){
        for(contact a : cc){
            list<string> VarList1 = a.abc__c.split(';'); 
            list<string> VarList2 = a.xyz__c.split(';'); 
            set<string> VarSetAdd1 = new set<string>();
            VarSetAdd1.addAll(VarList1);
            set<string> VarSetAdd2 = new set<string>();
            VarSetAdd2.addAll(VarList2);
            
            if(VarSetAdd1.containsAll(VarSetAdd2)){
                a.addError('error');
                
            }
        }
    }
}

Thanks,
Maharajan.C

 
Harshal Katole 7Harshal Katole 7
i want to use CONTAINS() function  insted of ContainsAll() here,.how can i achieved ??
   


public class checkdupcontact2{
    public static void invoke(list<contact> cc){
        for(contact a : cc){
             if( a.abc__c != null && a.xyz__c != null ){
                 
            list<string> VarList1 =a.abc__c.split(';'); 
            list<string> VarList2 =a.xyz__c.split(';'); 
            set<string> VarSetAdd1 = new set<string>();
            VarSetAdd1.addAll(VarList1);
            set<string> VarSetAdd2 = new set<string>();
            VarSetAdd2.addAll(VarList2);
            
           
                if(VarList1.contains(VarList2)){
                a.addError('error');
            }
          }     
        }
    }
}