You need to sign in to do that
Don't have an account?
Whitney 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();
}
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();
}
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();
}
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();
}
You could also do the same check in the existing if statement, but it will probably be simpler to keep the check separate.
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.