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
Whitney Klein 10Whitney Klein 10 

Add condition to trigger

Hello, I have a trigger that works fine except it is doing it for every instance. I want to have the trigger happen only on tasks where the Quick Call Log field is checked (Quick_Call_Log__c = True). Where should I add this to the trigger below? Thanks for any help in advance!! 

trigger TaskTrg on Task( after insert, after update ) 
{
    Map<Id, Account> accountsToBeUpdated = new Map<Id, Account>();
    
    for( Task t : trigger.new )
    {
        if( trigger.isInsert
            || ( trigger.isUpdate
                && ( trigger.oldMap.get( t.Id ).Call_Date__c != t.Call_Date__c
                    || trigger.oldMap.get( t.Id ).Description__c != t.Description__c )
                )
            )
        {
            if( t.WhatId != null && String.valueOf( t.WhatId ).startsWith( '001' ))
            {
                Account acc = new Account( Id = t.WhatId );
                acc.Last_Call_Date__c = t.Call_Date__c;
                acc.Last_Call_Description__c = t.Description__c;
                
                accountsToBeUpdated.put( acc.Id, acc );
            }
        }
    }
    
    if( accountsToBeUpdated.values().size() > 0 )
        update accountsToBeUpdated.values();
}
Daniel BallingerDaniel Ballinger
You can directly check for this condition in the for loop as you examine each task. E.g.
 
trigger TaskTrg on Task( after insert, after update ) 
{
    Map<Id, Account> accountsToBeUpdated = new Map<Id, Account>();
    
    for( Task t : trigger.new )
    {
        if(!t.Quick_Call_Log__c) 
        {
            // When Quick_Call_Log__c is false for the task in question we will continue to the next task
            continue;
        }

        // You other existing code here ....
    }
    
    if( accountsToBeUpdated.values().size() > 0 )
        update accountsToBeUpdated.values();
}

 
Whitney Klein 10Whitney Klein 10
Thank you so much for your help! I tried adding that if statement and continue statement. I run the trigger and it is still updating the fields from the tasks with the Quick Call Log = False. 
Whitney Klein 10Whitney Klein 10
Code now looks like this after suggested changes. I am an apex novice. 

trigger TaskTrgWithCheckbox on Task( after insert, after update ) 
{
    Map<Id, Account> accountsToBeUpdated = new Map<Id, Account>();
    
    for( Task t : trigger.new )
    {
        if(!t.Quick_Call_Log__c)
       
        {
        if(trigger.isInsert
            || ( trigger.isUpdate
                && ( trigger.oldMap.get( t.Id ).Call_Date__c != t.Call_Date__c
                    || trigger.oldMap.get( t.Id ).Description__c != t.Description__c )
                )
            )
             {continue;
        }
        {
            if( t.WhatId != null && String.valueOf( t.WhatId ).startsWith( '001' ))
            {
                Account acc = new Account( Id = t.WhatId );
                acc.Last_Call_Date__c = t.Call_Date__c;
                acc.Last_Call_Description__c = t.Description__c;
                
                accountsToBeUpdated.put( acc.Id, acc );
            }
        }
    }
    }
    
    if( accountsToBeUpdated.values().size() > 0 )
        update accountsToBeUpdated.values();
}
Whitney Klein 10Whitney Klein 10
tried this version as well and had same problem: 

trigger TaskTrgWithCheckbox on Task( after insert, after update ) 
{
    Map<Id, Account> accountsToBeUpdated = new Map<Id, Account>();
   for( Task t : trigger.new )
    {
            if(!t.Quick_Call_Log__c)
        {
            // When Quick_Call_Log__c is false for the task in question we will continue to the next task
            continue;
        }
        {
        if(trigger.isInsert
            || ( trigger.isUpdate
                && ( trigger.oldMap.get( t.Id ).Call_Date__c != t.Call_Date__c
                    || trigger.oldMap.get( t.Id ).Description__c != t.Description__c )
                )
            )
        {
            if( t.WhatId != null && String.valueOf( t.WhatId ).startsWith( '001' ))
            {
                Account acc = new Account( Id = t.WhatId );
                acc.Last_Call_Date__c = t.Call_Date__c;
                acc.Last_Call_Description__c = t.Description__c;
                
                accountsToBeUpdated.put( acc.Id, acc );
            }
        }
    }
    }
    
    if( accountsToBeUpdated.values().size() > 0 )
        update accountsToBeUpdated.values();
}
Daniel BallingerDaniel Ballinger
Try structuring the class as follows. Now the first thing it will do in the loop is check if Quick_Call_Log__c is false. If it is false, the code will move to the next Task.

You could also do the same check in the existing if statement, but it will probably be simpler to keep the check separate.
 
trigger TaskTrg on Task( after insert, after update ) 
{
    Map<Id, Account> accountsToBeUpdated = new Map<Id, Account>();
    
    for( Task t : trigger.new )
    {
        if(!t.Quick_Call_Log__c)
        {
             // When Quick_Call_Log__c is false for the task in question we will continue to the next task
             continue;
        }

        if( trigger.isInsert
            || ( trigger.isUpdate
                && ( trigger.oldMap.get( t.Id ).Call_Date__c != t.Call_Date__c
                    || trigger.oldMap.get( t.Id ).Description__c != t.Description__c )
                )
            )
        {
            if( t.WhatId != null && String.valueOf( t.WhatId ).startsWith( '001' ))
            {
                Account acc = new Account( Id = t.WhatId );
                acc.Last_Call_Date__c = t.Call_Date__c;
                acc.Last_Call_Description__c = t.Description__c;
                
                accountsToBeUpdated.put( acc.Id, acc );
            }
        }
    }
    
    if( accountsToBeUpdated.values().size() > 0 )
        update accountsToBeUpdated.values();
}

 
Whitney Klein 10Whitney Klein 10
Copy and pasted this into the trigger and tested it. Still having the same issue. When tasks have the quick call as false, it is still updating the account fields. The problem is since those fields are blank on the next task, it is making the account fields blank
Daniel BallingerDaniel Ballinger
That sounds odd. There is most likely something else at play here. You can try debugging the trigger to see what is going on. Probably the simplest way to do this is to add something like the following:
 
System.debug(LoggingLevel.INFO, 'TaskTrg Quick_Call_Log__c:' + t.Quick_Call_Log__c);
Try placing this just inside the for loop. Then you can look at the resulting log and verify that the expected action occured.

Alternatively, you can (and should) create an Apex test case that verifies the expected behavior. Have several test methods that insert and update Tasks that are linked to Accounts via a WhatId and have the Quick_Call_Log__c, Call_Date__c and Description__c fields populated with various values.