+ Start a Discussion
Brian LeedyBrian Leedy 

Handling multipick values in List/Map from SOQL statement

Attached is a trigger to grab values from the On_Call_Schedule object which stores the values I grab. I'm attempting to convert the location__c field to a mulitpick picklist. I've succeeded in the conversion of the actual field but now need converting the trigger to handle the possible multi value String in the List. As you can see, location__c is mapped with the Id of the On_Call_Schedule so that later is can be called to populate the Incident object that triggers it. I feel like it would be easiest to somehow store the multivalue string as a Set then parse it later. I've also considered moving the Maps and Lists under the Trigger.New statement so that I can just grab the Queue name from the Incident and add basically 'AND location__c = d.Owner.Id' to the SOQL for onCallList.

Disclaimer: I didn't originally write this so I may not be able to defend 'Why'. I've renamed variables and added notation for my sanity.
trigger OnCallTrigger on BMCServiceDesk__Incident__c (before insert, before update) {

    DateTime myTime = system.now();
	On_Call_Schedule__c OCSched = null;
    String location = '';
    
    //Create list of all possible on call schedules based on time period
    Map<String, On_Call_Schedule__c> onCallMap = new  Map<String, On_Call_Schedule__c>();
    List<On_Call_Schedule__c> onCallList = [select primary_on_call_assignee__c,
                                            secondary_on_call_assignee__c,
                                            primary_pager_pin__c,
                                            secondary_pager_pin__c,
                                            location__c
                                            from On_Call_Schedule__c
                                        	where (start_date_time__c <= :myTime)
                                        	and (end_date_time__c >= :myTime)];
    //For each on call schedule in list, add the location and Id to map
    Set<String> locationSet = new Set<String>();
    for(On_Call_Schedule__c d : onCallList) {
        onCallMap.put(d.location__c, d);
    }
    
    //Create list of all queues
	Map<id, string> QueueMap = new Map<id, string>();
    List<Group> grouplst = [select Name, Id 
                             from Group 
                             where TYPE = 'Queue' 
                             LIMIT 5000];
    //For each queue in list, add Id and Name to map
    for(Group grp : grouplst)
   		QueueMap.put(grp.id, grp.name);
    
    //For each triggered incident
    for(BMCServiceDesk__Incident__c d : Trigger.new) {
        
        string strQueue = ''; //Queue name
        
        //Set queueid to ticket 'owner' (user or queue) Id
        string queueid = d.OwnerId;
        //Check queue map to see if owner is queue (and not user)
        if(QueueMap.containsKey(queueid))
            strQueue = QueueMap.get(queueid);
        else
            strQueue = 'Service Desk';
        
        //Assigns prim/sec callers to incident 
        if(onCallMap.isEmpty() || !onCallMap.containsKey(strQueue))
			return;        
        else {
            OCSched = onCallMap.get(strQueue);
            d.Primary_On_Call_Assignment__c = OCSched.Primary_On_Call_Assignee__c;
        	d.Secondary_On_Call_Assignment__c = OCSched.Secondary_On_Call_Assignee__c;
            d.Primary_Pager_PIN_email_field__c = OCSched.Primary_Pager_PIN__c;
            d.Secondary_Pager_PIN_email_field__c = OCSched.Secondary_Pager_PIN__c;
        }
	}
}

 
Best Answer chosen by Brian Leedy
Alex SelwynAlex Selwyn
Moving SOQL under Trigger.new loop could break the governor limit.

See if this 
for(On_Call_Schedule__c d : onCallList) {
        List<String> locList = d.location__c.split(',');
        for(String loc : locList) {
             onCallMap.put(loc, d);
        }
        
 }

 

All Answers

Alex SelwynAlex Selwyn
Moving SOQL under Trigger.new loop could break the governor limit.

See if this 
for(On_Call_Schedule__c d : onCallList) {
        List<String> locList = d.location__c.split(',');
        for(String loc : locList) {
             onCallMap.put(loc, d);
        }
        
 }

 
This was selected as the best answer
Brian LeedyBrian Leedy
So simple! I had to modify it to split(';'); because that's how it's stored when entered from the picklist and then work through some errors but I'm on to the next improvement! Thanks!