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
Anne Clisham 7Anne Clisham 7 

Trigger creating more than one Task

I am using a trigger to create a task on an Account record.  The task should be created when a "notice" date on the account equals today's date.  I have the trigger working but every time I edit the account, it creates another task.  I only want one task for when the date criteria is met.  The notice date will change to a future date so the process will start all over once the new notice date equals today.  I read a few of the posts but I am not seeing something that matches what I am trying to do.  I am an admin but working to get more involved with developing. Thanks for any help.

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
trigger WaiveTaskCreate on Account (after update) {
   for (Account acct : Trigger.new){
   if(acct.Date_Notice__c==System.Today()){
       Task t = new Task();
       t.Subject    = 'Subject';
       t.OwnerID = acct.OwnerID;
       t.Priority   = 'High';
       t.Description    = 'Comment';
       t.WhatID     = acct.Id;
       t.ActivityDate = (acct.Exp_Date__c + 15);
       insert t;
}
}
}

 
Sumit Kumar Singh 9Sumit Kumar Singh 9
Hello Anne, 
You can try this - 
trigger WaiveTaskCreate on Account (before update) {
   List<Task> taskList = new List<Task>(); 
   for (Account acct : Trigger.new) {
	   if(acct.Date_Notice__c == Date.Today()) {
		   Task t = new Task();
		   t.Subject    = 'Subject';
		   t.OwnerID = acct.OwnerID;
		   t.Priority   = 'High';
		   t.Description    = 'Comment';
		   t.WhatID     = acct.Id;
		   t.ActivityDate = (acct.Exp_Date__c + 15);
		   insert t;
		   taskList.add(t);
		}
	}
	if(taskList!=null && taskList.size()>0) {
		insert taskList;
	}
}
Pls, let me know if it helps you. 

Thanks,
Sumit Kumar Singh
 
Deepak GulianDeepak Gulian
trigger WaiveTaskCreate on Account (before update) {
   List<Task> taskList = new List<Task>(); 
   for (Account acct : Trigger.new) {
	   if(acct.Date_Notice__c == Date.Today()) {
		   Task t = new Task();
		   t.Subject    = 'Subject';
		   t.OwnerID = acct.OwnerID;
		   t.Priority   = 'High';
		   t.Description    = 'Comment';
		   t.WhatID     = acct.Id;
		   t.ActivityDate = (acct.Exp_Date__c + 15);
		   taskList.add(t);
		}
	}
	if(taskList!=null && taskList.size()>0) {
		insert taskList;
	}
}

Never perform dml operation inside loop.
Try this!

Neetu_BansalNeetu_Bansal
Hi Anne,

Why are you writing trigger for this, the best way to resolve this is to use Time Based Workflows. As it will not create multiple task when you update the Account and accordingly create new task when the Notice Date is changed.

Try using the Time based workflows and if you face any issue or need assistance, please let me know.

Thanks,
Neetu
Anne Clisham 7Anne Clisham 7
The time based worked.  I was trying to get the records that already populated to update and they weren't so I just reloaded them with dataloader. Problem solved. Thank you.