+ Start a Discussion
Angela SchloederAngela Schloeder 

Trigger on Contact to create Task when field value is changed.

I'm trying to create a trigger to create a task everytime the field 'Call Results' is changed. This is what I have, but doesn't work. I am not a developer by any means. Copied this from another trigger and trying to make it work.

trigger NewCreateTask on Contact (after insert, after update) {
    List<Task> listOfTaskToBeInserted = new List<Task>();
    Task newtask;
    
    if(trigger.isInsert){
        for(Contact con : trigger.new){
            if(con.Call_Result__c == 'N/A' || con.Call_Result__c == 'Requested More Info' || con.Call_Result__c == 'Left voicemail' || con.Call_Result__c == 'No Answer or VM' || con.Call_Result__c == 'Not Interested' || con.Call_Result__c == 'Wants Call Back' || con.Call_Result__c == 'Set Meeting'){
                newtask = new Task();
                newtask.WhatId = con.AccountId;
                newtask.Subject = 'Task';
                newtask.whoId = con.id;
                newtask.ActivityDate = system.today();
                newTask.Status = 'Completed';
                {
                 listOfTaskToBeInserted.add(newTask);
               }
             }
          }
       }
      if(listOfTaskToBeInserted.size() > 0){
     upsert listOfTaskToBeInserted;  
  }
}
 
Best Answer chosen by Angela Schloeder
Raj VakatiRaj Vakati
trigger NewCreateTask on Contact (after update) {
List<Task> listOfTaskToBeInserted = new List<Task>();

Map<Id,Contact> mapOfId = Trigger.oldMap ; 

if(trigger.isUpdate){
	for(Contact con : trigger.new){
	if(mapOfId.get(con.Id).Call_Result__c != con.Call_Result__c){
	
		Task newtask = new Task();
		newtask.WhatId = con.AccountId;
		newtask.Subject = 'Task';
		newtask.whoId = con.id;
		newtask.ActivityDate = system.today();
		newTask.Status = 'Completed';
	   
		 listOfTaskToBeInserted.add(newTask);
	   }
	   }
	 }
  

if(listOfTaskToBeInserted.size() > 0){
insert  listOfTaskToBeInserted;  
}
}