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
SV MSV M 

Merging two functionalities into one trigger

Hi, I have two different triggers on the opportunity. One will update the Total Tasks custom field and another one creates a new task when the opportunity stage is set to 'Closed Won'. Can someone help me how to merge these two functionalities into one trigger...

//Trigger 1
trigger OpportunityTaskCount on Task (after insert, after update, after delete, after undelete) {
    Set<Id> oppList = new Set<Id>();
    Set<Id> tskList = new Set<Id>();
    List<Opportunity> updateList = new List<Opportunity>();
    if(trigger.isInsert || trigger.isUndelete) {
        for(Task tsk : trigger.new) {
            if(tsk.WhatId != NULL) {
                oppList.add(tsk.WhatId);
                tskList.add(tsk.Id);
            }
        }
    }
    if(trigger.isDelete || trigger.isUpdate) {
        for(Task tsk : trigger.old) {
            if(tsk.WhatId != NULL) {
                tskList.add(tsk.WhatId);
            }
        }
    }
    for (Opportunity opp : [SELECT Id, Name, Total_Tasks__c ,(SELECT Id, Status FROM Tasks) 
                            FROM Opportunity 
                            WHERE Id in : oppList]) {
                                opp.Total_Tasks__c = opp.Tasks.size();
                                updateList.add(opp);
                            }
    update updateList;
}

//Trigger 2
trigger ClosedOpportunityTrigger on Opportunity (before insert, before update) {
    List<Task> taskList = new List<Task>();
    for(Opportunity opp : trigger.new) {
        if(trigger.isInsert || trigger.isUpdate) {
            if(opp.StageName == 'Closed Won') {
                Task tsk = new Task();
                tsk.Subject = 'Follow Up Test Task';
                tsk.WhatId = opp.Id;
                taskList.add(tsk);
            }
        }
    }
    if(taskList.size() > 0) {
        insert taskList;
    }
}

Thanks in advance :)
ANUTEJANUTEJ (Salesforce Developers) 
Hi Sai Vineeth,

You could usr something like below to merge the two triggers that work on the same  object.
 
trigger triggername on object(all contexts)
{
if(tigger.isafter)
{
if(trigger.isnsert || trigger.isupdate || trigger.isdelete || trigger.isundelete)
{//in here you could use same conditional lines as above to execute a function as per your use case.}
}
if(trigger.isbefore)
{
if(tigger.isinsert|| trigger.isupdate)
//in here you could use same conditional lines as above to execute a function as per your use case.
}
}



You can learn more above above variables in https://trailhead.salesforce.com/en/content/learn/modules/apex_triggers/apex_triggers_intro

I hope this helps and in case if there are any questions do let me know or in case if this came handy can you please choose this as best answer so that it can be used by others in the future.

Regards,
Anutej