+ Start a Discussion
Whitney Klein 10Whitney Klein 10 

Apex Trigger to Update Account Fields from Activity History

Hello, I have made a quick action on my accounts tab that allows faster Log a Call. I have four 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 from log a call).

The tricky part is I also have Last SERVICE Call Date and Last SERVICE Call Description. I'd like these fields updated based on the user/profile from the quick 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!! 
Best Answer chosen by Whitney Klein 10
Neetu_BansalNeetu_Bansal
Hi Whitney,

For Service calls fields, use this code. Means this code will help you in Service calls as well as Account fields.

trigger TaskTrg on Task( before insert, after insert, after update ) 
{
    if( trigger.isBefore )
    {
        Profile p = [ Select Id, Name from Profile where Id =: UserInfo.getProfileId() ];
        
        for( Task t : trigger.new )
        {
            if( p.Name.equalsIgnoreCase( 'Sales Profile' ))
            {
                t.Description = 'Sales Profile';
            }
            else if( p.Name.equalsIgnoreCase( 'Customer Service Profile' ))
            {
                t.Description = 'Customer Service Profile';
            }
        }
    }
    
    if( trigger.isAfter )
    {
        Map<Id, Account> accountsToBeUpdated = new Map<Id, Account>();
        
        for( Task t : trigger.new )
        {
            if( trigger.isInsert
                || ( trigger.isUpdate
                    && ( trigger.oldMap.get( t.Id ).Start_Date__c != t.Start_Date__c
                        || trigger.oldMap.get( t.Id ).Description != t.Description )
                    )
                )
            {
                if( t.WhatId != null && String.valueOf( t.WhatId ).startsWith( '001' ))
                {
                    Account acc = new Account( Id = t.WhatId );
                    acc.Last_Sales_Call_Date__c = t.Start_Date__c;
                    acc.Last_Sales_Call_Description__c = t.Description;
                    
                    accountsToBeUpdated.put( acc.Id, acc );
                }
            }
        }
        
        if( accountsToBeUpdated.values().size() > 0 )
            update accountsToBeUpdated.values();
    }
}

Thanks,
Neetu

All Answers

Neetu_BansalNeetu_Bansal
Hi Whitney,

I actually did not get you requirement, how would you like to populate Account fields. Can you please elaborate your requirement.

Thanks,
Neetu
Whitney Klein 10Whitney Klein 10
Hi Neetu, 

Everytime a call is created on the account object using a quick call button I made, I want certain fields to be updated on the account page with the information from the call. 

Field on Account - Field from Call
SALES PROFILE
Last Sales Call Date - Start Date
Last Sales Call Description - Description (custom field)

CUSTOMER SERVICE PROFILE
Last Service Call Date - Start Date
Last Service Call Description - Description (custom field)

The call(task object) only has one description field and I would like to update the fields based on the user's profile ID. 

I tried to do this using the Process Builder with no success and I think a trigger is needed. Thanks for any help!! 
Neetu_BansalNeetu_Bansal
Hi Whitney,

I am assuming the API Name for Start Date of Call as Start_Date__c, Last Sales Call Date as Last_Sales_Call_Date__c and Last Sales Call Description as Last_Sales_Call_Description__c.

Please use this:
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 ).Start_Date__c != t.Start_Date__c
                    || trigger.oldMap.get( t.Id ).Description != t.Description )
                )
            )
        {
            if( t.WhatId != null && String.valueOf( t.WhatId ).startsWith( '001' ))
            {
                Account acc = new Account( Id = t.WhatId );
                acc.Last_Sales_Call_Date__c = t.Start_Date__c;
                acc.Last_Sales_Call_Description__c = t.Description;
                
                accountsToBeUpdated.put( acc.Id, acc );
            }
        }
    }
    
    if( accountsToBeUpdated.values().size() > 0 )
        update accountsToBeUpdated.values();
}

Thanks,
Neetu
Whitney Klein 10Whitney Klein 10
Hi Neetu, 

I am very new to triggers. Where should I place this code and how? Sorry for being such a beginner! Thanks! 
Whitney Klein 10Whitney Klein 10
Also, Is there a piece in this trigger for the Service call fields as well? Thanks! 
Neetu_BansalNeetu_Bansal
Hi Whitney,

Follow these steps:
Go to setup -> Customize -> Activities -> Task Triggers.
Click New and paste this code.

For Service calls, you want to enter different Description, on the basis of profiles? Is the Description fixed or it will vary always?

Thanks,
Neetu
Whitney Klein 10Whitney Klein 10
Excellent, Thanks! I want to enter the same description field (there is only one) but based on the user's profile ID put Sales Profile in Sales Fields and Customer Service Profile in the Service Fields. Thank you so much for your patience and help! I really appreciate it!!! 
Neetu_BansalNeetu_Bansal
Hi Whitney,

For Service calls fields, use this code. Means this code will help you in Service calls as well as Account fields.

trigger TaskTrg on Task( before insert, after insert, after update ) 
{
    if( trigger.isBefore )
    {
        Profile p = [ Select Id, Name from Profile where Id =: UserInfo.getProfileId() ];
        
        for( Task t : trigger.new )
        {
            if( p.Name.equalsIgnoreCase( 'Sales Profile' ))
            {
                t.Description = 'Sales Profile';
            }
            else if( p.Name.equalsIgnoreCase( 'Customer Service Profile' ))
            {
                t.Description = 'Customer Service Profile';
            }
        }
    }
    
    if( trigger.isAfter )
    {
        Map<Id, Account> accountsToBeUpdated = new Map<Id, Account>();
        
        for( Task t : trigger.new )
        {
            if( trigger.isInsert
                || ( trigger.isUpdate
                    && ( trigger.oldMap.get( t.Id ).Start_Date__c != t.Start_Date__c
                        || trigger.oldMap.get( t.Id ).Description != t.Description )
                    )
                )
            {
                if( t.WhatId != null && String.valueOf( t.WhatId ).startsWith( '001' ))
                {
                    Account acc = new Account( Id = t.WhatId );
                    acc.Last_Sales_Call_Date__c = t.Start_Date__c;
                    acc.Last_Sales_Call_Description__c = t.Description;
                    
                    accountsToBeUpdated.put( acc.Id, acc );
                }
            }
        }
        
        if( accountsToBeUpdated.values().size() > 0 )
            update accountsToBeUpdated.values();
    }
}

Thanks,
Neetu
This was selected as the best answer
Whitney Klein 10Whitney Klein 10
Hello Neetu, 

I was working on this again and the code you gave me works perfectly for the last call description/task description but it will not pull in the last call date from the due date of the task. Instead it pulls in 11/30/15 4:00 PM each time. Any help is greatly appreciated!

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();
}
Whitney Klein 10Whitney Klein 10
figured it out, the activity date API is actually connected to two different fields. I did a custom Call Date field and added it to the quick action and trigger and it worked!!!
Neetu_BansalNeetu_Bansal
Hi Whitney,

Perfect that it works, you can contact me anytime for any help. My details are mention on my profile.

Thanks,
Neetu