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
Mahmoud Coudsi 1Mahmoud Coudsi 1 

Automating task assignment through Apex Trigger

Problem:

I created an Apex trigger that evaluates multiple conditions on the Accounts object (through if statements). If the conditions are met then the trigger creates a task related to the Accounts (the ones that meet the conditions). Now, since the trigger is from the (After Update type), tasks are being created everytime the account is updated (edited and saved). Which is creating tasks duplication 

Solution:

I tried to create a list of tasks that were created today() through a SOQL query then write an IF statement inside the trigger's loop that would exclude those accounts (that had tasks created today()) from the loop; don't create tasks for them..

However, after I added TodaysTasks list and the conditional statement (to avoid duplication) to the code, the tasks stopped getting assigned completely..(The highlighted code is the part that was added to avoid duplication)

User-added image
 
trigger AddTask on Account(after update) {
    // Variable that returns a date; 30 days from today.
    Date DateThirtyDaysAgo = date.today().addDays(-30); 
    
   // Create a task list that includes all the tasks
    List <Task> TaskList = new List<Task>();
    
   // Create a tasklist that includes all the tasks that are created today
    List<Task> TodaysTask = [SELECT accountid from Task Where Task_Created_Date__c = Today];
    
    // Get the related Task for the accounts in this trigger
    Map <Id,Account> AcctsWithTask = new Map <Id,Account>(
        [SELECT Id,(SELECT Id FROM Tasks) FROM Account WHERE Id IN :Trigger.New]);
    
    // Add an Task for each account if it doesn't already have one.
    // Iterate through each account.
    for(Account a : Trigger.New) {
        if (a.Next_appointment_date__c == date.today() && a.Latest_BIA_date__c < DateThirtyDaysAgo) {
            // If it doesn't, add a task
            TaskList.add(new Task(subject= 'This is a test',
                                  Description= 'This is a test',
                                  ActivityDate = Date.today().addDays(1),
                                  WhatID = a.Id));
        }           
    }
    
    if (TodaysTask.size() < 1) {
        insert TaskList;
        }











 
Best Answer chosen by Mahmoud Coudsi 1
Maharajan CMaharajan C
Hi Mahmoud,

Please try the below code:

trigger AddTask on Account(before update,after update) {
    // Variable that returns a date; 30 days from today.
    Date DateThirtyDaysAgo = date.today().addDays(-30); 
 // Create a task list that includes all the tasks
    
    List <Task> TaskList = new List<Task>();
    
    set<Id> AccIds=new set<Id>();
    for(Account a:trigger.new)
    {
    AccIds.add(a.Id);
    }
  
   // Create a tasklist that includes all the tasks that are created today
   List<Task> TodaysTask =new List<Task>([SELECT Id,whatId from Task Where whatId in :AccIds AND CreatedDate=TODAY]);
    
    // Get the related Task for the accounts in this trigger
    //Map <Id,Account> AcctsWithTask = new Map <Id,Account>([SELECT Id,(SELECT Id FROM Tasks) FROM Account WHERE Id IN :AccIds]);
    
    // Add an Task for each account if it doesn't already have one.
    // Iterate through each account.
    for(Account a : Trigger.New) {
    
        if (a.Next_appointment_date__c == date.today() && a.Latest_BIA_date__c < DateThirtyDaysAgo&&TodaysTask.size() < 1) {
            // If it doesn't, add a task
            TaskList.add(new Task(subject= 'This is a test forum',
                                  Description= 'This is a test',
                                  ActivityDate = Date.today().addDays(1),
                                  WhatID = a.Id));
        }           
    }
    
    
        insert TaskList;
        }

Let me know if it works or not!!!

If it works mark this as a best answer!!!

Thanks,
​Raj

All Answers

Maharajan CMaharajan C
Hi Mahmoud,

Please try the below code:

trigger AddTask on Account(before update,after update) {
    // Variable that returns a date; 30 days from today.
    Date DateThirtyDaysAgo = date.today().addDays(-30); 
 // Create a task list that includes all the tasks
    
    List <Task> TaskList = new List<Task>();
    
    set<Id> AccIds=new set<Id>();
    for(Account a:trigger.new)
    {
    AccIds.add(a.Id);
    }
  
   // Create a tasklist that includes all the tasks that are created today
   List<Task> TodaysTask =new List<Task>([SELECT Id,whatId from Task Where whatId in :AccIds AND CreatedDate=TODAY]);
    
    // Get the related Task for the accounts in this trigger
    //Map <Id,Account> AcctsWithTask = new Map <Id,Account>([SELECT Id,(SELECT Id FROM Tasks) FROM Account WHERE Id IN :AccIds]);
    
    // Add an Task for each account if it doesn't already have one.
    // Iterate through each account.
    for(Account a : Trigger.New) {
    
        if (a.Next_appointment_date__c == date.today() && a.Latest_BIA_date__c < DateThirtyDaysAgo&&TodaysTask.size() < 1) {
            // If it doesn't, add a task
            TaskList.add(new Task(subject= 'This is a test forum',
                                  Description= 'This is a test',
                                  ActivityDate = Date.today().addDays(1),
                                  WhatID = a.Id));
        }           
    }
    
    
        insert TaskList;
        }

Let me know if it works or not!!!

If it works mark this as a best answer!!!

Thanks,
​Raj
This was selected as the best answer
Maharajan CMaharajan C
Can i have any update!!!
Mahmoud Coudsi 1Mahmoud Coudsi 1
Hi Raj!

Thanks for your contribution. It works great! I'm just trying to add on more conditions in TodaysTask: Where Subject includes "Forum"

 List<Task> TodaysTask =new List<Task>([SELECT Id,whatId from Task Where whatId in :AccIds AND CreatedDate=TODAY AND Subject include "Forum"]);

Can you guide me through this?
Maharajan CMaharajan C
Hi Mahmoud,

Try Like below:

List<Task> TodaysTask =new List<Task>([SELECT Id,whatId from Task Where whatId in :AccIds AND CreatedDate=TODAY AND subject like '%Forum%' ]);

Let me know if it works or not!!!

If it works mark this as a best answer!!!

Thanks,
​Raj
 
Mahmoud Coudsi 1Mahmoud Coudsi 1
Raj, that was very helpful thanks a lot! Let me know if you are on Linkedin, would love to connect!
Maharajan CMaharajan C
Thank You!!! Glad to help you!!!

My LinkedIn Profile: https://www.linkedin.com/in/maha-rajan-1ba40b109/

Skype: maharaja0393

Thanks,
Raj