+ Start a Discussion
Whitney Klein 10Whitney Klein 10 

How do I create an Apex Trigger to Update Account Fields from A Quick Action

Hello, I have made a quick action on my accounts tab that allows faster Log a Call. I have two fields on my account that I want to have updated with this quick call info. The fields are Last Sales Call Date (date of log a call) and Last Sales Call Description (description field from log a call).

I know I need to do this with a trigger as opposed to workflow rules but I have absolutely no experience with Apex. Any help is greatly appreciated!! 
sharathchandra thukkanisharathchandra thukkani
You can create trigger on task object and update the fields on account object.
Whitney Klein 10Whitney Klein 10
Excellent, do you have any example code you could help me to write the trigger? I have no experience with triggers and am really struggling. 
Whitney Klein 10Whitney Klein 10
Here is the code I have now - it is working for the description field and last call description but the date keeps pulling in 11/30/15 4:00 PM

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 ).ActivityDate != t.ActivityDate
                    || 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.ActivityDate;
                acc.Last_Call_Description__c = t.Description__c;
                
                accountsToBeUpdated.put( acc.Id, acc );
            }
        }
    }
    
    if( accountsToBeUpdated.values().size() > 0 )
        update accountsToBeUpdated.values();
}