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
JayDP123JayDP123 

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

 

Best Answer chosen by Admin (Salesforce Developers) 
EnthEnth

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 

t.purpose__c += ';Sway Cards;';

is flawed. Why the trailing ; in the string?

 

All Answers

EnthEnth

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 

t.purpose__c += ';Sway Cards;';

is flawed. Why the trailing ; in the string?

 

This was selected as the best answer
JayDP123JayDP123

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.  

 

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 "Sway Cards", then 
			the type of the task will change to Email. And the due date of the task will
			be for the next coming Wednesday.
			*/
 
    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);           
            
    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 : trigger.new){
        Boolean result = t.subject.contains('Sway Card');
        System.debug(result);
        If (result == True){
            For (User u : bdmOwner){
                If (t.ownerID == u.id){
       			tskToUpdate.add(t);
                }
            }
            For (User u : bdeOwner){
                If (t.ownerID == u.id){
                    tskToUpdate.add(t);
                }
            }
            system.debug(tskToUpdate);
        }
    }
            
        If (tskToUpdate != null){
        For (Task t : tskToUpdate)
        {
            If (t.purpose__c.indexOf('Sway Cards') < 0){
                t.purpose__c += ';Sway Cards';
                system.debug(t.purpose__c);
            	}
            t.ActivityDate = nextWednesday;
            system.debug(t.ActivityDate);
        }
    	}
    }