+ Start a Discussion


Hello, I have a question that I thought somebody on here might be able to help me with.  Let me preface this by saying that I am completely new to Apex/Trigger coding in SF and how it is set up.  But I AM able to see code already constructed and decipher how it is working (I have some programming backgroung but no Apex/SF specific coding background).  Anyhow, to the question.  I am trying to create a field that will list the "Last Activity" for a case.  This is available already on Contacts and Accounts (I believe) but not on Cases.  I am sure I am not the first to deal with this issue and I am also pretty sure that the trigger for this would be very general (i.e. not Org specific) and the only differences might be what one would call an "Activity".  I really only want it to reflect the same as it does on a contact in regards to e-mails sent and received that are related to a specific case.  If someone could provide me with a set of basic trigger code for this I would be much obliged.  Also, I assume the trigger would reside on the activities object, correct?  Thanks in advance!


Jeremy Stender




Stebder - Any luck with this?  I am looking for the same thing.  We need Last Activity on Cases and then I want to set up notification emails for any cases that are not updated or no activity (last activity) in 7 days.  I'm curious if you ever resolved this.




Hi Lizks,


I went through this before I was an actual developer (have since become MUCH more of a developer) but I was able to find and edit a trigger that has worked for us.  


The code below is written to work for lot of different objects, but I have it commented out so that it will only fire when tasks related to Cases are created. (with letters telling me where the {} opened and closed, as this was before I had eclipse).  You'll see that you need to have a Date/Time field on cases with the API name Last_Activity_Datetime__c to make this work.


Just plop this on a trigger and it should work:

trigger NewTaskTouchParentObject on Task (after insert) {
    //Put these in maps to ensure that each object is only in there once
    Map<Id,Case> casesToUpdate = new Map<Id,Case>();
//    Map<Id,Lead> LeadsToUpdate = new Map<Id,Lead>();
//    Map<Id,Opportunity> oppsToUpdate = new Map<Id,Opportunity>();
//    Map<Id,Campaign> campsToUpdate = new Map<Id,Campaign>();
//    Map<Id,Contact> contsToUpdate = new Map<Id,Contact>();
//    Map<Id,Contract> contrsToUpdate = new Map<Id,Contract>();

    for (Task tsk:System.Trigger.new) {
        //Only CTI would set the CallType field
//a        if (tsk.CallType!=null && tsk.CallType!='') {
//b            if (tsk.WhoId!=null) {
//                String whoId = tsk.WhoId;
//              String whoIdPrefix = whoId.substring(0,3);
//c                if (whoIdPrefix.equalsIgnoreCase('00Q')) {
//                    Lead lead = new Lead(Id=tsk.WhoId, Last_Activity_Datetime__c=System.now());
//                    leadsToUpdate.put(tsk.WhoId, lead);
//c d               } else if (whoIdPrefix.equalsIgnoreCase('003')) {
//                    Contact lead = new Contact(Id=tsk.WhoId, Last_Activity_Datetime__c=System.now());
//                    contsToUpdate.put(tsk.WhoId, lead);
//d                }
//b            }
            if (tsk.WhatId!=null) {
                String whatId = tsk.WhatId;
                String whatIdPrefix = whatId.substring(0,3);

                //Only CTI would fill the CallType field
                if (whatIdPrefix.equalsIgnoreCase('500')) {
                  Case acc = new Case(Id=tsk.WhatId, Last_Activity_Datetime__c=System.now());

                    casesToUpdate.put(tsk.WhatId, acc);
//               } else if (whatIdPrefix.equalsIgnoreCase('006')) {
//                   Opportunity acc = new Opportunity(Id=tsk.WhatId, Last_Activity_Datetime__c=System.now());
//                 oppsToUpdate.put(tsk.WhatId, acc);
//                } if (whatIdPrefix.equalsIgnoreCase('701')) {
//                    Campaign acc = new Campaign(Id=tsk.WhatId, Last_Activity_Datetime__c=System.now());
//                   campsToUpdate.put(tsk.WhatId, acc);
//                } if (whatIdPrefix.equalsIgnoreCase('800')) {
//                    Contract acc = new Contract(Id=tsk.WhatId, Last_Activity_Datetime__c=System.now());
//                    contrsToUpdate.put(tsk.WhatId, acc);
//a        }



Hope that helps,

Jeremy Stender


Thanks so much for this!  I am slowly teaching myself how to write Triggers and Vf, but when I start from scratch it takes me forever!  I really appreciate the prompt reply and code share.  I'm trying this out now...