You need to sign in to do that
Don't have an account?
![JayDP123 JayDP123](https://dfc-org-production.my.site.com/img/userprofile/default_profile_45_v2.png)
Updating ActivityDate and MultiPicklist
Heya,
I have a trigger to update ActivityDate field and Purpose__c multiselect picklist when a Task is Inserted or Updated. All of my system.debugs come up with the correct value, but nothing gets updated. Should I put it in an After Insert/Update maybe? I don't know why the fields aren't getting updated.
trigger Task_SwayCard_AssignTask on Task (before insert, before update) { /* A trigger on the Task object that if the Task Owner is BDM or BDE, and the Subject of the task is like "Sway Card", then the type of the task will change to Email, the due date of the task will be for the next coming Wednesday, and Sway Cards will be added to purpose__c if it isn't already there.*/ List<Task> tskSway = [SELECT id, ownerId, subject, purpose__c from Task where subject like '%Sway Card%' and id =: trigger.new]; // A list to hold "Sway Cards" tasks Date myDate = date.today(); Date sunday = myDate.toStartOfWeek(); Date nextSunday = sunday.addDays(7); Integer dayOfWeek = sunday.daysBetween(myDate); Date thisWednesday = sunday.addDays(3); Date nextWednesday = nextSunday.addDays(3); System.debug(myDate); System.debug(sunday); System.debug(nextWednesday); If (tskSway != NULL){ system.debug('tskSway is Not Nulll!!'); List<User> bdmOwner = [select id, name, userrole.name from user //A list to hold all BDMs in the org where userrole.name like '%BDM%']; List<User> bdeOwner = [select id, name, userrole.name from user where userrole.name like '%BDE%']; List<Task> tskToUpdate = new List<Task>();//A list to hold which tasks need to be updated For (Task t : tskSway) //Populate tskToUpdate based on whether the owner is a BDM or BDE, { //and whether the Task is in the tskSway list. If (bdmOwner != null) { For (User u : bdmOwner) { If (u.id == t.ownerID) { tskToUpdate.add(t); } } } If (bdeOwner != null) { For (User u : bdeOwner) { If (u.id == t.ownerID) { tskToUpdate.add(t); } } } } If (tskToUpdate != null){ System.debug('tskToUpdate is not Null!'); For (Task t : tskToUpdate) { If (t.purpose__c.indexOf('Sway Cards') <= 0){ system.debug('purpose has Sway Cards!' + t.purpose__c); t.purpose__c += ';Sway Cards;'; system.debug(t.purpose__c); } t.ActivityDate = nextWednesday; system.debug(t.ActivityDate); } } } }
I think you'd find it much easier to re-write this so that you loop through all the records in the trigger.new once, and within the loop check if the task is Sway Card and if the owner is in your lists (use maps instead and containsKey).
This will be much shorter, faster, easier to maintain and use less system resources.
BTW, I also think
is flawed. Why the trailing ; in the string?
All Answers
I think you'd find it much easier to re-write this so that you loop through all the records in the trigger.new once, and within the loop check if the task is Sway Card and if the owner is in your lists (use maps instead and containsKey).
This will be much shorter, faster, easier to maintain and use less system resources.
BTW, I also think
is flawed. Why the trailing ; in the string?
Thanks Enth.
I changed the code around so that it moostly fits into one larger loop. Also took out the extra ';' in the string, I guess I just wasn't sure whether SalesForce would put the ';' before or after the value when someone uses the User Interface. I took it from a different post anyway and so figured it was right. Anyway I took it out.
I didn't use any maps though. Would maps benefit me that much do you think?
Thanks again.
Here is the working code: although the trigger is still not 100% complete.