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
Rachel Linder 8Rachel Linder 8 

Created a Trigger for a Task - can you please review

Hello,
I wrote a trigger on the task object. What the trigger is supposed to accomplish is looking at a name in a field on the task, then going to a custom object to find that name, and when the name is found pull the email from that custom object onto the task. 
Can you please review my code below and let me know if this will accomplish what I need? Then I will need to deploy it. I know the process for how to deploy the trigger but do I need to create a test case first? And if so what would that test class look like?
____________________________________________________________________________________________________________________
TRIGGER
trigger TaskAccountManagerEmailTrigger on Task (before insert, before update) {

    map<string, string> DM = new map<string, string>();
    Set<String> Person = new Set<String>();
    For(Task t: trigger.new)
    {
        if(t.Account_Manager__c!=null)
        Person.add(t.Account_Manager__c);
    } 
    
    List<Holding_Object__c> HoldingLst = new List<Holding_Object__c>();
    if(Person!=null && Person.size()>0)
    {
       HoldingLst=[select Persons_Name__c, Email_Address__c from Holding_Object__c where Persons_Name__c in: Person];
    }
    
    if(HoldingLst!=null && HoldingLst.size()>0)
    {
        for(Holding_Object__c hd : HoldingLst)
        {
            DM.put(hd.Persons_Name__c, hd.Email_Address__c);
        }
    }
    
    for(Task t: trigger.new)
    {
        if(DM.Containskey(t.Account_Manager__c) && DM.get(t.Account_Manager__c)!=null)
        t.Account_Manager_Email__c = DM.get(t.Account_Manager__c);
    }
    
}
Best Answer chosen by Rachel Linder 8
Alain CabonAlain Cabon
Hi,

It is difficult to verify your code without all the definitions of the used fields but that seems quite good (bulkified).

This trigger is called when there are creations or updates of tasks.
So if another test already creates or updates a task, this trigger will be called but the code coverage could be low if Account_Manager__c is never initialized by this test for instance. You need also an inserted instance of Holding_Object__c initialized with the correct values.
You can reuse another test that already creates tasks and you just add the field Account_Manager__c for one task, plus the creation of at least one Holding_Object__c.

All Answers

Alain CabonAlain Cabon
Hi,

It is difficult to verify your code without all the definitions of the used fields but that seems quite good (bulkified).

This trigger is called when there are creations or updates of tasks.
So if another test already creates or updates a task, this trigger will be called but the code coverage could be low if Account_Manager__c is never initialized by this test for instance. You need also an inserted instance of Holding_Object__c initialized with the correct values.
You can reuse another test that already creates tasks and you just add the field Account_Manager__c for one task, plus the creation of at least one Holding_Object__c.
This was selected as the best answer
Rachel Linder 8Rachel Linder 8
Thanks Alain. All went well.