+ Start a Discussion
QD93QD93 

Apex Trigger: Cross Object Field Update on Task WhatID from related Contact(WhoId)

New to Apex...

Whenever a Task is created, I'd like to relate the Task's WhatID to the related Contact's (whoid) lookup field (custom field) TriggerTest__c. 

I have been receivin this error message : Error: Compile Error: Cannot save a trigger during a parse and save class call at line -1 column -1
 
trigger TaskRelatedToContact on Task (after insert,after update) {


Set<Id> ContactIds = new Set<Id>(); for(Task t : trigger.new)
{String wId = t.WhoId; 
if(wId!=null && wId.startsWith('003') && 
   !ContactIds.contains(t.WhoId)) 
    
    { ContactIds.add(t.WhoId); } }


List<Contact> 
taskContacts = [Select Id, TriggerTest__c 
from Contact where Id in :ContactIds]; 
Map<Id, Contact> ContactMap = new Map<Id, Contact>(); 
for(CONTACT c : taskContacts)
{ ContactMap.put(c.Id,c); } 


for(Task t : trigger.new){ String wId = t.WhoId; 
if(wId!=null && wId.startswith('003')){CONTACT thisContact = ContactMap.get(t.WhoId); 
if(thisContact!=null){t.WhatId = thiscontact.TriggerTest__c;} 
} 
} 
}

 
Best Answer chosen by QD93
<Saket><Saket>
trigger TaskRelatedToContact on Task (before insert, before update) {
    
    // Goal: Find the Contact ID of the Contact that a task is related to 
    // and update the Whatid. field on the task object with the value Triggertest__c
    
    // If related to a contact, query to find out the triggertest__c 
    // Create collection of tasks that are related to a contact (where the contact is listed only once) 
    
    Set<Id> ContactIds = new Set<Id>(); 
    for(Task t : trigger.new)
    {
        String wId = t.WhoId; 
        if(wId!=null && wId.startsWith('003') && 
           !ContactIds.contains(t.WhoId)){ ContactIds.add(t.WhoId); 
                                         } 
    } 
    
    
    // Pull in Contact ids and related field to populate task record 
    
    
    List<Contact> taskContacts = [Select Id, TriggerTest__c 
                                  from Contact where Id in :ContactIds]; 
    Map<Id,Contact> ContactMap = new Map<Id,Contact>(); 
    for(CONTACT c : taskContacts)
    { 
        ContactMap.put(c.Id,c); 
    } 
    
    // Update whatid task field with custom Contact field 
    
    for(Task t : trigger.new){
        String wId = t.WhoId; 
        if(wId!=null && wId.startswith('003')){
            CONTACT thisContact = ContactMap.get(t.WhoId); 
            if(thisContact!=null){
                t.WhoId = thiscontact.TriggerTest__c;
            } 
        } 
    } 
}

Try this 

All Answers

<Saket><Saket>
Hi Quan Try This
 
trigger TaskRelatedToContact on Task (after insert, after update) {
    
    // Goal: Find the Contact ID of the Contact that a task is related to 
    // and update the Whatid. field on the task object with the value Triggertest__c
    
    // If related to a contact, query to find out the triggertest__c 
    // Create collection of tasks that are related to a contact (where the contact is listed only once) 
    
    Set<Id> ContactIds = new Set<Id>(); 
    for(Task t : trigger.new)
    {
        String wId = t.WhoId; 
        if(wId!=null && wId.startsWith('003') && 
           !ContactIds.contains(t.WhoId)){ ContactIds.add(t.WhoId); 
                                         } 
    } 
    
    
    // Pull in Contact ids and related field to populate task record 
    
    
    List<Contact> taskContacts = [Select Id, TriggerTest__c 
                                  from Contact where Id in :ContactIds]; 
    Map<Id,Contact> ContactMap = new Map<Id,Contact>(); 
    for(CONTACT c : taskContacts)
    { 
        ContactMap.put(c.Id,c); 
    } 
    
    // Update whatid task field with custom Contact field 
    
    for(Task t : trigger.new){
        String wId = t.WhoId; 
        if(wId!=null && wId.startswith('003')){
            CONTACT thisContact = ContactMap.get(t.WhoId); 
            if(thisContact!=null){
                t.WhoId = thiscontact.TriggerTest__c;
            } 
        } 
    } 
}

if problem get solved then please marked this as a best answer
QD93QD93
Hi Saket,

Unfortunately, still receiving the same error message. Error: Compile Error: Cannot save a trigger during a parse and save class call at line -1 column -1
<Saket><Saket>
Are u using a developer console if yes then try it saving from normal salesforce editor 
in quick find box go to -> apex trigger then edit and save this code which I provided u 
QD93QD93
I am using Dev Console and confirming I can save the Apex Trigger now thanks. Unfortunately, the code still does not work. 

Note: the TriggerTest__c is a lookup field from the Contact record to a custom object. I am receiving this error:

User-added image

Is it because I can't use After Insert and After Update on the same 'Task record' that the trigger was on to update a lookup field (whatid)? 

 
QD93QD93
I am receiving the error message when attempting to create a new Task with the related contact record.
<Saket><Saket>
Thanks for confirming

Chnage the event do it in before for both the events update and insert?
<Saket><Saket>
trigger TaskRelatedToContact on Task (before insert, before update) {
    
    // Goal: Find the Contact ID of the Contact that a task is related to 
    // and update the Whatid. field on the task object with the value Triggertest__c
    
    // If related to a contact, query to find out the triggertest__c 
    // Create collection of tasks that are related to a contact (where the contact is listed only once) 
    
    Set<Id> ContactIds = new Set<Id>(); 
    for(Task t : trigger.new)
    {
        String wId = t.WhoId; 
        if(wId!=null && wId.startsWith('003') && 
           !ContactIds.contains(t.WhoId)){ ContactIds.add(t.WhoId); 
                                         } 
    } 
    
    
    // Pull in Contact ids and related field to populate task record 
    
    
    List<Contact> taskContacts = [Select Id, TriggerTest__c 
                                  from Contact where Id in :ContactIds]; 
    Map<Id,Contact> ContactMap = new Map<Id,Contact>(); 
    for(CONTACT c : taskContacts)
    { 
        ContactMap.put(c.Id,c); 
    } 
    
    // Update whatid task field with custom Contact field 
    
    for(Task t : trigger.new){
        String wId = t.WhoId; 
        if(wId!=null && wId.startswith('003')){
            CONTACT thisContact = ContactMap.get(t.WhoId); 
            if(thisContact!=null){
                t.WhoId = thiscontact.TriggerTest__c;
            } 
        } 
    } 
}

Try this 
This was selected as the best answer
QD93QD93
Thanks!. Works. Below is the sample code.
 
trigger TaskRelatedToContact on Task (before insert, before update) {
    
    // Goal: Find the Contact ID of the Contact that a task is related to 
    // and update the Whatid. field on the task object with the value Triggertest__c
    
    // If related to a contact, query to find out the triggertest__c 
    // Create collection of tasks that are related to a contact (where the contact is listed only once) 
    
    Set<Id> ContactIds = new Set<Id>(); 
    for(Task t : trigger.new)
    {
        String wId = t.WhoId; 
        if(wId!=null && wId.startsWith('003') && 
           !ContactIds.contains(t.WhoId)){ ContactIds.add(t.WhoId); 
                                         } 
    } 
    
    
    // Pull in Contact ids and related field to populate task record 
    
    
    List<Contact> taskContacts = [Select Id, TriggerTest__c 
                                  from Contact where Id in :ContactIds]; 
    Map<Id,Contact> ContactMap = new Map<Id,Contact>(); 
    for(CONTACT c : taskContacts)
    { 
        ContactMap.put(c.Id,c); 
    } 
    
    // Update whatid task field with custom Contact field 
    
    for(Task t : trigger.new){
        String wId = t.WhoId; 
        if(wId!=null && wId.startswith('003')){
            CONTACT thisContact = ContactMap.get(t.WhoId); 
            if(thisContact!=null){
                t.WhatId = thiscontact.TriggerTest__c;
            } 
        } 
    } 
}

 
<Saket><Saket>
Yup 

I m so happy that it worked, Thanks for marking my answer as a best answer :)

Thanks
Saket Sharma
sharmasaket703@gmail.com