+ Start a Discussion
Linked2MarkLinked2Mark 

Task Trigger throwing error - Saying Task is read only?

Anyone have an idea why I am unable to edit the value of a Task field in a trigger? I have checked the Field Level Security on the 'Type' field and it is visible and is not marked as Read Only. Yet when the trigger fires - it throws an error when I say - Trigger.new[i].Type = 'Email';

Many thanks for your help.
trigger Task_UpdateLastContact on Task (after insert, after update) {
	public boolean weShouldUpdateContacts = Validator_cls.runUpdateContactsFromTasks('get', true);
	if(weShouldUpdateContacts){
	    Set<Id> neededRT = new Set<Id>();
	    neededRT.addAll(zTest_Shared.GetRecordTypeIdBySObjectAndName('Task', new Set<String>{
	    	'ESI - Inside Sales Task',
	    	'ESI - Outbound Call Log',
	    	'ESI - Task'
	    }).values());
	    Set<Id> newTriggerIds = new Set<Id>();
	  	system.debug('neededRT.contains(Trigger.new[0].RecordTypeId) is ' + neededRT.contains(Trigger.new[0].RecordTypeId));
	
	    if(neededRT.contains(Trigger.new[0].RecordTypeId)){    
	
	        String oldStatus = '';
	    
	        for (Integer i = 0; i < Trigger.new.size(); i++){
					
	                if (Trigger.isInsert)
	                    oldStatus = '';
	                    if(Trigger.new[i].Description.contains('Additional To:')){
	                    	system.debug('<== This came from Outlook ==>');
	                    	Trigger.new[i].Type = 'Email';
	                    }
	                else
	                    oldStatus = Trigger.old[i].Status;
	                
	                //Create the set of Ids that get passed to the Future method
	             
	                if((Trigger.new[i].Status == 'Completed - Effective' //small dash
	                  || Trigger.new[i].Status == 'Completed – Effective' //large dash
	                  || Trigger.new[i].Status == 'Completed')
	                && Trigger.new[i].Status != oldStatus)
	                {
	            		newTriggerIds.add(Trigger.new[i].Id);
	                }
	
	        }       
	    }
	    updateLastContacted.updateLastContacted(newTriggerIds);
	}
}

 
Best Answer chosen by Linked2Mark
Raj VakatiRaj Vakati
YOU Can't perform DML on after insert or update trigger directly ..  Change is to before events 
 
trigger Task_UpdateLastContact on Task (before insert, before update) {
	public boolean weShouldUpdateContacts = Validator_cls.runUpdateContactsFromTasks('get', true);
	if(weShouldUpdateContacts){
	    Set<Id> neededRT = new Set<Id>();
	    neededRT.addAll(zTest_Shared.GetRecordTypeIdBySObjectAndName('Task', new Set<String>{
	    	'ESI - Inside Sales Task',
	    	'ESI - Outbound Call Log',
	    	'ESI - Task'
	    }).values());
	    Set<Id> newTriggerIds = new Set<Id>();
	  	system.debug('neededRT.contains(Trigger.new[0].RecordTypeId) is ' + neededRT.contains(Trigger.new[0].RecordTypeId));
	
	    if(neededRT.contains(Trigger.new[0].RecordTypeId)){    
	
	        String oldStatus = '';
	    
	        for (Integer i = 0; i < Trigger.new.size(); i++){
					
	                if (Trigger.isInsert)
	                    oldStatus = '';
	                    if(Trigger.new[i].Description.contains('Additional To:')){
	                    	system.debug('<== This came from Outlook ==>');
	                    	Trigger.new[i].Type = 'Email';
	                    }
	                else
	                    oldStatus = Trigger.old[i].Status;
	                
	                //Create the set of Ids that get passed to the Future method
	             
	                if((Trigger.new[i].Status == 'Completed - Effective' //small dash
	                  || Trigger.new[i].Status == 'Completed – Effective' //large dash
	                  || Trigger.new[i].Status == 'Completed')
	                && Trigger.new[i].Status != oldStatus)
	                {
	            		newTriggerIds.add(Trigger.new[i].Id);
	                }
	
	        }       
	    }
	    updateLastContacted.updateLastContacted(newTriggerIds);
	}
}

 

All Answers

Raj VakatiRaj Vakati
YOU Can't perform DML on after insert or update trigger directly ..  Change is to before events 
 
trigger Task_UpdateLastContact on Task (before insert, before update) {
	public boolean weShouldUpdateContacts = Validator_cls.runUpdateContactsFromTasks('get', true);
	if(weShouldUpdateContacts){
	    Set<Id> neededRT = new Set<Id>();
	    neededRT.addAll(zTest_Shared.GetRecordTypeIdBySObjectAndName('Task', new Set<String>{
	    	'ESI - Inside Sales Task',
	    	'ESI - Outbound Call Log',
	    	'ESI - Task'
	    }).values());
	    Set<Id> newTriggerIds = new Set<Id>();
	  	system.debug('neededRT.contains(Trigger.new[0].RecordTypeId) is ' + neededRT.contains(Trigger.new[0].RecordTypeId));
	
	    if(neededRT.contains(Trigger.new[0].RecordTypeId)){    
	
	        String oldStatus = '';
	    
	        for (Integer i = 0; i < Trigger.new.size(); i++){
					
	                if (Trigger.isInsert)
	                    oldStatus = '';
	                    if(Trigger.new[i].Description.contains('Additional To:')){
	                    	system.debug('<== This came from Outlook ==>');
	                    	Trigger.new[i].Type = 'Email';
	                    }
	                else
	                    oldStatus = Trigger.old[i].Status;
	                
	                //Create the set of Ids that get passed to the Future method
	             
	                if((Trigger.new[i].Status == 'Completed - Effective' //small dash
	                  || Trigger.new[i].Status == 'Completed – Effective' //large dash
	                  || Trigger.new[i].Status == 'Completed')
	                && Trigger.new[i].Status != oldStatus)
	                {
	            		newTriggerIds.add(Trigger.new[i].Id);
	                }
	
	        }       
	    }
	    updateLastContacted.updateLastContacted(newTriggerIds);
	}
}

 
This was selected as the best answer
Raj VakatiRaj Vakati
http://sfdcsrini.blogspot.com/2014/09/what-is-record-is-read-only-trigger.html
Linked2MarkLinked2Mark
Duh... Why didnt I see that? Thanks so much.