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 

omparing 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.

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');
            }
          }     
        }
    }
}
CharuDuttCharuDutt
Hii Harshal Katole
Try Below Code
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);
                 
                 for(Integer i=0;i < VarSetAdd1.size();i++){
    if(VarSetAdd1[i].Contains(VarSetAdd2[i])){
         a.addError('error');
               }
            }  
          }     
       }
    }
}
Please Mark It As Best Answer If It Helps
Thank You!

 
Harshal Katole 7Harshal Katole 7
1.here contain() function returns only LIST value. so made some correction please check code
2.got "checdupContacttri: execution of BeforeUpdate caused by: System.ListException: List index out of bounds: 3 Class.checkdupcontact2.invoke: line 17, column 1 Trigger.checdupContacttri: line 6, column 1" error    while inserting abc__c = 1,2,5,7 and xyz__c = 2,3,5

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(';'); 
                 system.debug(VarList1);
            list<string> VarList2 =a.xyz__c.split(';');
                 system.debug(VarList2);
            /*set<string> VarSetAdd1 = new set<string>();
            VarSetAdd1.addAll(VarList1);
            set<string> VarSetAdd2 = new set<string>();
            VarSetAdd2.addAll(VarList2);*/
            
           
                 for(Integer i=0;i < VarList1.size();i++){
    if(VarList1[i].Contains(VarList2[i])){
                a.addError('error');
            }
          }     
        }
      }
    }
}