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
Satish9Satish9 

question on upsert task

Having a bit of trouble with this trigger. need some help please...

I want to create a task when a checkListItem is updated. Task should upserted in case existing task for the checklist is not closed. This code is always creating a new task irrespective of existing taks. How to fix this? 

//test

trigger AssignCalendarTask  on Checklist_Item__c (before update){ 
	
	List<Task> tasks = [Select id, whatid, status from task where status <>'Closed']; 
	
	List<Checklist_Item__c> Itms = Trigger.new; 
		
		for (Checklist_Item__c Itm : Itms) {
				
				If (Itm.Active__c && Itm.Responsible__c <> null && Itm.Next_Activity__c <> null) {
				
					Task tsk = new Task(whatID = Itm.ID,
					Ownerid = Itm.Responsible__c,
					Subject = Itm.Activity_Type__c,
					Status = 'Assigned',
					Description = 'This is a system assigned Task.', 
					ActivityDate = Itm.Next_Activity__c,
					ReminderDateTime=Itm.Next_Activity__c - 3,
					IsReminderSet=true);  
		
		tasks.add(tsk); } 
	
		}
		
		upsert tasks; 
		
}



robdobbyrobdobby
Hi!  Could you please clarify "Task should upserted in case existing task for the checklist is not closed."  So you want to close any existing Tasks before creating new one?
Satish9Satish9
hello Rob !

I want to create a new task only if there isn't an existing task. For others I want to update task where Checklist_Item__c.Id = Task.whatid.