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?
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);
}
}