You need to sign in to do that
Don't have an account?
isalew
Display Last Activity Subject on Lead Object
SF Admin new to Apex.
I am trying to create a field on the Lead object (and eventually contact/account objects) to display the subject field from the last activity. I have already figured out how to display the last activity date, but I'm stuck on how to display the last activity subject. The field should pull from both Task & Event records.
Any suggestions?
Thank you for your help, Tim. You sent me in the right direction. I played with the code some more and found a working solution:
All Answers
Here is my Apex so far. Could be totally off. I borrowed heavily from this post:
http://www.radialweb.com/2010/08/summarizing_salesforce_fields_with_triggers/
Areas where I have no idea where to go are noted with the word LOGIC.
//------------------------------------------------------------------------------------------------
//Apex Class
//------------------------------------------------------------------------------------------------
public with sharing class LeadLastActivitySubject {
public static String ldPrefix = Lead.sObjectType.getDescribe().getKeyPrefix();
public static void updateLeadLastActivitySubject(Set<ID> leadIds){
//Query all Leads and task-event child relationships
List<Lead> Leads = [SELECT Id, Last_Activity_Subject__c, (SELECT Id, Subject, ActivityDate FROM Tasks order by ActivityDate Desc limit 1), (SELECT Id, Subject, ActivityDate FROM Events order by ActivityDate Desc limit 1) FROM Lead WHERE ID IN :leadIds]
List<Lead> updateLeads = new List<Lead>();
for (Lead l : Leads) {
//LOGIC to determine most recent task or event and update Lead.Last_Activity_Subject__c field
}
try {
update updateLeads;
}
catch (Exception e) {
}
}
}
//------------------------------------------------------------------------------------------------
//Apex Task Trigger
//------------------------------------------------------------------------------------------------
trigger TaskUpdateLeadLastActivitySubject on Task (after delete, after insert, after undelete, after update) {
Set<ID> ldIds = new Set<ID>();
//Isolate Tasks linked to leads
String prefix = LeadLastActivitySubject.ldPrefix;
//Adds lead ids coming from new data
if (Trigger.new != null) {
for (Task t : Trigger.new) {
if (t.WhoId != null && String.valueOf(t.whoId).startsWith(prefix) ) {
ldIds.add(t.whoId);
}
}
}
//Adds lead ids coming from old data (deletes, moving an activity from one lead to another)
if (Trigger.old != null) {
for (Task t : Trigger.old) {
if (t.WhoId != null && String.valueOf(t.whoId).startsWith(prefix) ) {
ldIds.add(t.whoId);
}
}
}
//LOGIC MAY BE WRONG HERE
if (ldIds.size() > 0)
LeadLastActivitySubject.updateLeadLastActivitySubject(ldIds);
}
//------------------------------------------------------------------------------------------------
//Apex Event Trigger
//------------------------------------------------------------------------------------------------
trigger EventUpdateLeadLastActivitySubject on Event (after delete, after insert, after undelete, after update) {
Set<ID> ldIds = new Set<ID>();
//Isolate Events linked to leads
String prefix = LeadActivityCount.ldPrefix;
//Adds lead ids coming from new data
if (Trigger.new != null) {
for (Event e : Trigger.new) {
if (e.WhoId != null && String.valueOf(e.whoId).startsWith(prefix) ) {
ldIds.add(e.whoId);
}
}
}
//Adds lead ids coming from old data (deletes, moving an activity from one opportunity to another)
if (Trigger.old != null) {
for (Event e : Trigger.old) {
if (e.WhoId != null && String.valueOf(e.whoId).startsWith(prefix) ) {
ldIds.add(e.whoId);
}
}
}
//LOGIC MAY BE WRONG HERE
if (ldIds.size() > 0)
LeadLastActivitySubject.updateLeadLastActivitySubject(ldIds);
}
I think you are looking for how to iterate through child objects from your SOQL query. Hope the below snippet helps.
Thank you Tim, that helps, but I'm trying to go one step further, to compare the date and event activity dates so the latest task or event will display. I tried to adapt your code, but my adaptation didn't work.
Any suggestions regarding comparative logic?
Thank you for your help, Tim. You sent me in the right direction. I played with the code some more and found a working solution:
first of all thanks ilewi
but this still doesnt work for me, did u change somtehing else? e.g. in the trigger? The customfield Last_Activity_Subject_c ist Type text, isn't it?
Hello flor1an,
Yes, I modified the class. Here is the code I developed for the activity subject & date. The trigger should be the same, with some changes to variable/class names.
Thanks for your answer! But i'm afrais to say it still doesn't work for me.. I'm just interested in the subject of the Activity, so the "old trigger" you posted in the beginning still should work i guess, but it doesnt -.-
could you maybe check the trigger once again?
Thank you very much so far...
Sorry to hear it's not working flor1an. I'm not sure how to help without knowing the nature of your problem. Did the developer console give you any error messages that might indicate why it's not working?
I should mention that the code I displayed is cut from a larger project, so will not work with just cut-and-paste. You will need to adapt it to fit your project.
I am Admin and new to coding. I have created similar scenario in my org and using your Oct 15, 2013 code you posted. I am getting an error at line 32 "Variable does not exist: TotalCount". Where should I declare that variable? I also wanted to know after the Apex class is created, can I use the task and event triggers you have mentioned in inital question?