1

I have a custom field on task and event called Type with values as Call, Voicemail, Email..

I have a custom field on lead object, called last activity type. Now I want to populate this field on the lead detail page. Is there any standard way to do this.

Thoughts? Ideas?

Thanks

4
  • trigger is the fastest answer but i would prefer thinking to find have i missed anything else .Since task or event not master detail so cross object formula field wont apply. Commented Sep 15, 2013 at 15:34
  • A simple trigger would be the best way to accomplish this. I know you asked about a standard way, which I dont know if there is. I can post some code for the trigger if you are willing to try it with a trigger. Commented Sep 15, 2013 at 16:12
  • Yes, can you please post some code. Meanwhile I am also looking for a standard way. Commented Sep 15, 2013 at 16:36
  • Added some code below Commented Sep 15, 2013 at 18:19

1 Answer 1

1

Something Like this should work.

trigger ExampleTrigger on Task (after insert) {

    set<Id> leadIds = new  set<Id>();
    for(Task t: trigger.new){
        if(t.Who.Type == 'Lead'){
            leadIds.add(t.WhoId);
        }
    }
    list<Lead> leadsToUpdate = new list<Lead>([Select Id, Name, Last_Activity_Type, (Select Id, ActivityDate, Type__c From Tasks) From Lead Where Id in : leadIds]);

    //This will cycyle through all the leads
    for(Lead l: leadsToUpdate){
        Date latestActivity;

            /*This will go through each task related to the current lead and find the 
            task with the latest date, and change the Last_Activity_Date__c fieild
            on that lead to the type of that latest activity*/

            for(Task t : l.Tasks){
            if (latestActivity == null || lastestActivity < t.ActivityDate){
                latestActivity = t.ActivityDate;
                l.Last_Activity_Type__c = t.Type__c;
            }
        }
    }

    update leadsToUpdate;

}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .