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
srikanth gubbala 1srikanth gubbala 1 

How to i check if a multi picklist is having a following value or not in apex coding

Example:-

   Set<id> AccIdList = new Set<id>();
    List<Account> accountsToUpdate = new List<Account>();
    List<String> conNames = new List<String>();
    for(Contact con : contacts){
        if(con.PartnerDesignation__c== 'Principal'||(con.PartnerDesignation__c!=oldMap.get(con.Id).PartnerDesignation__c && con.PartnerDesignation__c!='Principal')){
        AccIdList.add(con.accountid);
        }
    }   

This works fine if multipicklist is having only one value but ifts having picklist "Princpal" and "Secondary" its not working 
Best Answer chosen by srikanth gubbala 1
AnkaiahAnkaiah (Salesforce Developers) 
Hi Srikanth,

you can use contains method.

try with below code.
 
Set<id> AccIdList = new Set<id>();
    List<Account> accountsToUpdate = new List<Account>();
    List<String> conNames = new List<String>();
    for(Contact con : contacts){
        if(con.PartnerDesignation__c.contains('Principal')||(con.PartnerDesignation__c.contains('Principal')!=oldMap.get(con.Id).PartnerDesignation__c.contains('Principal') && (!con.PartnerDesignation__c.contains('Principal')))){
        AccIdList.add(con.accountid);
        }
    }

If this helps, Please mark it as best answer.

Thanks!!
 

All Answers

AnkaiahAnkaiah (Salesforce Developers) 
Hi Srikanth,

you can use contains method.

try with below code.
 
Set<id> AccIdList = new Set<id>();
    List<Account> accountsToUpdate = new List<Account>();
    List<String> conNames = new List<String>();
    for(Contact con : contacts){
        if(con.PartnerDesignation__c.contains('Principal')||(con.PartnerDesignation__c.contains('Principal')!=oldMap.get(con.Id).PartnerDesignation__c.contains('Principal') && (!con.PartnerDesignation__c.contains('Principal')))){
        AccIdList.add(con.accountid);
        }
    }

If this helps, Please mark it as best answer.

Thanks!!
 
This was selected as the best answer
srikanth gubbala 1srikanth gubbala 1
Thanks very much @Ankaiah