1

I am trying to compare multi-select picklist old value vs new values after the update. The new values should not have old values. For example, in the below case, only the "MA" value needs to be updated.

Problem # 1 When I tried to query for old attendees values ("CK" and "PK"), I kept getting all the values ("CK", "PK" and "MA"). Problem # 2 Comparing two sets of initials and adding it to the new set. Is it possible?

enter image description here

Trigger

trigger cloneEventTrigger on Event (after update) {
if(trigger.isUpdate) {
        cloneEventHelper.onAfterUpdate(trigger.new,trigger.old);
    }  
 } 

Helper class:

public static void onAfterUpdate(List<Event> newEvnt, List<Event> oldEvnt){  
        // get all fields from Events
        List<Id>eventIds = new List<Id>();
        for(Event evnt : [SELECT Id From Event WHERE id=:newEvnt AND Attendees__c != NULL])
            { 
                if (evnt.Id != null)
                {
                    eventIds.add(evnt.Id);
                    System.debug('EventIDs' + eventIds);
                }          
            }
       Event[] queryEvent = [SELECT Id, Attendees__c From Event WHERE id=:eventIds];
       Event[] oldQueryEvent = [ SELECT Id, Attendees__c From Event WHERE id=:oldEvnt];

     // Problem # 1- why oldqueryEvent is not pulling old Attendees list? 


        Map<String, Id> initialsToUserId = new Map<String, Id>();
        Set<String> newInitials = new Set<String>();
        Set<String> oldInitials = new Set<String>();
        Set<String> initials = new Set<String>(); //to hold only new initials
        Map<id,User> updateAll= new Map<id, User>();

       for(Event record: queryEvent) {
            newInitials.addAll(record.Attendees__c.split(';'));
        }  
        for(Event record: oldQueryEvent) {
            oldInitials.addAll(record.Attendees__c.split(';'));
        }

   // Problem # 2
       for(String init: oldInitials){
    if(!newInitials.contains(init)){ 
   //Method does not exist or incorrect signature: void addAll(String) from the type Set<String>
           initials.addAll(init);
      }      
     } 

  // The Next step is to use the initial picklist set to pull user records.
   for(User record: [SELECT Initials__c FROM User WHERE Initials__c = :initials AND IsActive = TRUE]) 
   {
           initialsToUserId.put(record.Initials__c, record.Id);
    }
    }
1

Answer to Problem #1:

In order to get the old Attendees list, you have to use the Trigger.oldMap instead of Trigger.old. So change your trigger class to use trigger.oldMap.

trigger cloneEventTrigger on Event (after update) {
    if(trigger.isUpdate) {
        cloneEventHelper.onAfterUpdate(trigger.new, trigger.oldMap);
    }
}

Now you want to query for the oldMap record in your helper method so that you can compare it with the new record. To do that you can call the get method using the event salesforce id.

public static void onAfterUpdate(List<Event> newEvnt, Map<Id, Event> oldEvntMap){
    for (Event e : newEvnt) {
        if (e.Attendees__c != null) {
            // should print 'oldEven: CK;PK'
            system.debug('oldEven: ' + oldEvntMap.get(e.Id).Attendees__c); 
            // should print 'newEven: CK;PK;MA'
            system.debug('newEven: ' + e.Attendees__c);  
        } 
    }
}

Answer to Problem #2:

The addAll method only accepts list or set as paramter. You should use add method instead of addAll method in your case.

Reference: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_set.htm

| improve this answer | |
  • Thanks, Jason. That's exactly what I was looking for. – mike Apr 2 at 13:46
  • Jason, how you loop through oldEvntMap.get(e.Id).Attendees__c and add them to set? – mike Apr 2 at 16:18
  • You can do it while you are in the for (Event e : newEvnt) loop. – Jason Fung Apr 2 at 18:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.