+ Start a Discussion
Kristen Hudgens 9Kristen Hudgens 9 

Cross Object Field Update Trigger

I have the need to update a field on a case, based on some values when a task is created on the case.

If the Subject of the task is 'Package Sent to Owner', I need to update the field OwnerPackageSentDate__c (on the case) to Todays date.

Any help is appreciated.
Mudasir WaniMudasir Wani
Hello Kristen,

Which action of trigger are you using after update or before update?
 
Mudasir WaniMudasir Wani
Another solution will be that you use a future method.
Remember you can pass primitive data types only as parameters.
@future 
public static methodname(){
//do the stuff here 
}
Nandan NarasappaNandan Narasappa
Below code would meet your requirement :

trigger TaskTrigger on Task (after insert) {
    List<Case> caseList = new List<Case>();
    for(Task tsk : Trigger.new){
        if(tsk.whoId != null && tsk.whoId.getSObjectType() == Case.sObjectType && 'Package Sent to Owner'.equals(tsk.Subject)){
            Case tskCase = new Case(Id = tsk.whoId);
            tskCase.OwnerPackageSentDate__c = Date.Today();
            caseList.add(tskCase);
        }
    }
    
    Database.update(caseList);
}

You would need to move this to a class & then invoke in trigger.