+ Start a Discussion
Whitney Klein 10Whitney Klein 10 

Update a trigger for Leads object

Hello, I got the following code to work on accounts but I need to have it do the same thing for leads. What do I need to update to make this happen? I tried putting Lead everywhere Account was but got an error that "leadsToBeUpdated" was invalid. Thanks for any help!!!


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();
}
SonamSonam (Salesforce Developers) 
The below lines should change if you wish to assign the task to the lead:

   if( t.WhatId != null && String.valueOf( t.WhatId ).startsWith( '001' ))
            {
                Account acc = new Account( Id = t.WhatId );

Lead is stored in the WhoID field and not WhatID so the code should handle that field instead:

   if( Whoid != null && String.valueOf( Whoid ).startsWith( '00Q' ))
            {
                Lead ld = new Lead( Id = t.Whoid );