You need to sign in to do that
Don't have an account?
Whitney 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!!
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!!
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
I actually did not get you requirement, how would you like to populate Account fields. Can you please elaborate your requirement.
Thanks,
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!!
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
I am very new to triggers. Where should I place this code and how? Sorry for being such a beginner! Thanks!
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
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
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();
}
Perfect that it works, you can contact me anytime for any help. My details are mention on my profile.
Thanks,
Neetu