function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
gastoniengastonien 

cross object trigger

I would like to write a trigger when task is completed and the case (a field) associated to the task send an email, trigger doesnt work when i add a field from a different object different from task:

.customer_Service_Agent__c is on case not task

trigger Notify_task_creator on task (after update) {
task t=[select CreatedBy.email, id, CreatedByid, CreatedBy.name, LastModifiedByid, LastModifiedBy.name, Subject
        from task 
        where id=:trigger.newMap.keySet()];

                        
for (task o : Trigger.new) 
{ 
if((o.Status=='Completed' ) && (o.CreatedByid != o.LastModifiedByid) && (o.CreatedByid != o.customer_Service_Agent__c)){ 

 MailerUtils.sendMail('<html><body>Hi ' + t.CreatedBy.name +', <br><br>' + t.LastModifiedBy.name + ' has completed the task: "' + t.Subject + '" <br><br>Click on this link for more information:</body></html>' + '\n' + 'https://p2-1147--Full.cs11.cloudforce.com/'+o.Id);
    }

} 
}


Best Answer chosen by gastonien
Mark TroupeMark Troupe
trigger Notify_task_creator on task (after update) {

List<Id> caseIds = new List<Id>();

for (task t : trigger.new){
    caseIds.add(t.whatId);
}

List<Case> cases = [select id, customer_Service_Agent__c from Case where id in: caseIds];

                        
for (task o : Trigger.new) 
{ 
    for (Case c : cases){
         if(c.Id == o.whatId && (o.Status=='Completed' ) && (o.CreatedByid != o.LastModifiedByid) && (o.CreatedByid != c.customer_Service_Agent__c)){ 

         MailerUtils.sendMail('<html><body>Hi ' + o.CreatedBy.name +', <br><br>' + o.LastModifiedBy.name + ' has completed the task: "' + o.Subject + '" <br><br>Click on this link for more information:</body></html>' + '\n' + 'https://p2-1147--Full.cs11.cloudforce.com/'+o.Id);
        }
    }
 } 
}
something like this ^^^

All Answers

Mark TroupeMark Troupe
You can't reference Case fields this way, you will need to create a List of Ids using the task.whoId field, then use this list to select the case related to the task.

Also why are you querying for the task using .keySet, and then looping through trigger.new? you only need to do one or the other.
gastoniengastonien
What do you suggest? Can you write a sample code?
Mark TroupeMark Troupe
trigger Notify_task_creator on task (after update) {

List<Id> caseIds = new List<Id>();

for (task t : trigger.new){
    caseIds.add(t.whatId);
}

List<Case> cases = [select id, customer_Service_Agent__c from Case where id in: caseIds];

                        
for (task o : Trigger.new) 
{ 
    for (Case c : cases){
         if(c.Id == o.whatId && (o.Status=='Completed' ) && (o.CreatedByid != o.LastModifiedByid) && (o.CreatedByid != c.customer_Service_Agent__c)){ 

         MailerUtils.sendMail('<html><body>Hi ' + o.CreatedBy.name +', <br><br>' + o.LastModifiedBy.name + ' has completed the task: "' + o.Subject + '" <br><br>Click on this link for more information:</body></html>' + '\n' + 'https://p2-1147--Full.cs11.cloudforce.com/'+o.Id);
        }
    }
 } 
}
something like this ^^^
This was selected as the best answer