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
Avinash Ravisankar 13Avinash Ravisankar 13 

Referencing multipicklist in Apex Trigger

Hi All,

I have a trigger to assign Contacts by criteria-based-Round Robin based on Project and Language parameters. The below code query was returning values as long as the Projects field was a picklist, now after I've converted it into a multipicklist, it doesn't seem to return any values. Please asist
 
trigger AssignSalesmanager on contact(before insert , after update)
{
    String currentuser = [Select alias From User Where Id = :UserInfo.getUserId()].alias;
            system.debug(currentuser);
            if(currentuser == 'guest' || currentuser == 'oadmi')
            {
    if(Trigger.isBefore)
    {
        if(Trigger.IsInsert)
        {
            Map<String,List<round_robin__c>> mapOfLangUsers = new  Map<String,List<round_robin__c>>();
            Map<String,List<round_robin__c>> mapOfProjUsers = new  Map<String,List<round_robin__c>>();
           
            map<string,list<contact>> MapoflangandContacts = new map<string,list<contact>>();
            map<string,list<contact>> MapofprojandContacts = new map<string,list<contact>>();
            Set<String> projectValues = new Set<String>();
            String projValues;
            Set<String> langValues = new Set<String>();
            List<Contact> allContacts = new List<contact>();
            Boolean hasModifiedUser = false;
            for(contact eachcontact: trigger.new){
  
               if(!String.isBlank(eachcontact.Language__c) && !String.isBlank(eachcontact.Projects__c)) {
                   allContacts.add(eachcontact);
                   langValues.add(eachcontact.language__c);
                   projectValues.add(eachcontact.projects__c);
                   projvalues= String.valueOf(projectValues);
                     
               }     
             
            }
            
            if(MapoflangandContacts.size() > 0 && MapoflangandContacts!= null) {
            for(string strlang : MapoflangandContacts.keyset()){
                integer i=0;
                for(Contact objContact : MapoflangandContacts.get(strlang)){
                    objContact.ownerid = mapOfLangUsers.get(objContact.Language__c)[i].Round_Robin_User__c;
                    mapOfLangUsers.get(objContact.Language__c)[i].Count__c = mapOfLangUsers.get(objContact.Language__c)[i].Count__c+1;
           
                    i++;
                if(i==mapOfLangUsers.get(objContact.Language__c).size())
                    i=0;
                }
            } 
           
            list<round_robin__c> lstrb = new list<round_robin__c>();
    
            for(list<round_robin__c> lstuser : mapOfLangUsers.values()){
                lstrb.addall(lstuser);
            }  
            update lstrb; 
           
           }
           if(MapofprojandContacts.size() > 0 && MapofprojandContacts!= null) {
               for(string strProject : MapofprojandContacts.keyset()){
                    integer i=0;
                    for(Contact objContact : MapofprojandContacts.get(strProject)){
                        objContact.ownerid = mapOfProjUsers.get(objContact.Projects__c)[i].Round_Robin_User__c;
                        mapOfProjUsers.get(objContact.Projects__c)[i].Count__c = mapOfProjUsers.get(objContact.Projects__c)[i].Count__c+1;
               
                        i++;
                    if(i==mapOfProjUsers.get(objContact.Projects__c).size())
                        i=0;
                    }
                } 
           
                list<round_robin__c> lstrb = new list<round_robin__c>();
        
                for(list<round_robin__c> lstuser : mapOfProjUsers.values()){
                    lstrb.addall(lstuser);
                }  
                update lstrb; 
           }
          
           if(allContacts.size() > 0 && allContacts!=null) {
           Integer recordCount = [select count() from contact];
           system.debug('recordCount='+recordCount);
           Integer rr = [select Count() from round_robin__c where Is_Active__c = TRUE AND Language__c IN:langValues AND Projects__c INCLUDES (:projvalues)   ];
             system.debug('rr='+rr);
             if(rr!=0)
           {
             Integer index = Math.mod(recordCount+1,rr);
            
           list <round_robin__c> lstRoundSpecificUsers  = [select id,name,Count__c,Round_Robin_User__c,Language__c,Projects__c from round_robin__c where Is_Active__c = TRUE AND Language__c IN:langValues AND Projects__c INCLUDES (:projvalues)   ];
               system.debug(lstRoundSpecificUsers  );
               Integer i;
               IF(i==null)
               {i=0;}

               for(contact eachContact : trigger.new) {
                   
                       system.debug('index='+index);
                     eachContact.ownerId = lstRoundSpecificUsers[index].Round_Robin_User__c;
              
                   
               }
               }  
           } 
        } 
      
     }   
     }
    
   if(trigger.isUpdate) { 
       System.debug('----->Entered');
        Map<Id,Id> mapOfContact = new Map<Id,Id>();
        for(Contact con: trigger.new)
         {
             
           if(con.OwnerId != trigger.oldmap.get(con.id).ownerId)
           {
               System.debug('----->Entered111');
              mapOfContact.put(con.Id,con.OwnerId);
          }
        }
        list<pba__Request__c> lstRequest = [select id,name,Contact_Owner__c,pba__Contact__c from pba__Request__c where pba__Contact__c IN:mapOfContact.keySet() ];
        if(lstRequest.size() > 0) {
            for(pba__Request__c eachReq : lstRequest) {
                eachReq.OwnerId = mapOfContact.get(eachReq.pba__Contact__c);
            }
        }
        update lstRequest;
    }
}


The system.debug statement for vairable rr keeps returning as 0 if I compare it with a set of values. It returns rows if I hardcode the values, which I don't want to. I want this to be dynamic.

How do i work around this.

Thanks,

Avinash

Nayana KNayana K
// variable blank 
String projValues = '';
Set<String> langValues = new Set<String>();
List<Contact> allContacts = new List<contact>();
Boolean hasModifiedUser = false;
for(contact eachcontact: trigger.new){

   if(!String.isBlank(eachcontact.Language__c) && !String.isBlank(eachcontact.Projects__c)) {
	   allContacts.add(eachcontact);
	   langValues.add(eachcontact.language__c);
	   projectValues.add(eachcontact.projects__c);
	   //concatinate with comma
	   projvalues+= String.valueOf(projectValues) + ',';
		 
   }     
 
}
projvalues = projvalues.removeEnd(',');

 
Saravana Bharathi 1Saravana Bharathi 1
There is a limitation in SOQL Binding Variables.
Cannot Bind Dynamic Variable in Static SOQL. [Select Id From Case Where Testing__c INCLUDES :(string)] ------This wont work.
We can use only Dynamic SOQL query and use INCLUDE (:<string>)-----We should use only by Database.query(query) to fetch the details.

I am giving an example:

Lets say Testing__C is a multi-select Picklist field with below values
1
2
3

Lets say, you want to query with all case, which is having 2 or 3.

Below code gives you the count.

        String check = '\'2\',\'3\'';
        List<Case> listOfCases = new List<Case>();
        String query = 'Select Id From Case Where Testing__c INCLUDES (';
        query = query +check+')';
listOfCases = Database.query(query);
        system.debug(' size ='+listOfCases.size());  

Hope. This Helps.

Thanks  
Amit GhadageAmit Ghadage
in first for loop

replace

projectValues.add(eachcontact.projects__c);

with

projectValues.addAll(eachcontact.projects__c.split(';'));