+ Start a Discussion
lalit kumar 11lalit kumar 11 

how to copy the task field value as on the associated lead field

Hi there,

I am trying to run a trigger for a while with different patterns but not getting the results except errors.
One custom field(value_to_be_copied__c) is on lead and one custom field(copied_value__c ) is on task.
The condition is whenever a task related to a lead is created, the value of value_to_be_copied__c should be copied to copied_value__c. 
First pattern is :-
trigger leadtasktrigger on Task (after insert) {

set<id> leid = new set<id>();
for(task ta :trigger.new){
if(string.valueof(ta.whoid).startsWith('OOQ'))
        {
          leid.add(ta.whoid);
         }
       }
 for(lead le2 :[select id, (select id, subject, copied_value__c from task), value_to_be_copied__c from lead where id in :leid]){
   le2.task.copied_value__c = le2.value_to_be_copied__c;
 }
}

Error for the above code is 
Error: Compile Error: Didn't understand relationship 'task' in FROM part of query call. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names. at line 10 column 16

Second pattern is -

trigger leadtasktrigger on Task (after insert) {

set<id> taid = new set<id>();
list<task> taid1 = new list<task>();
for(task ta :trigger.new){
if(string.valueof(ta.whoid).startsWith('OOQ'))
        {
          taid.add(ta.id);
         }
       }
   for(task ta1 : [select id, subject, copied_value__c, whoid from task where id in: taid]){
     
     taid1.add(ta1);
     
   } 
   
   for(task ta2 : taid){
   ta2.copied_value__c = ta2.who.value_to_be_copied__c ;
   }
 }
Error: Compile Error: Invalid field value_to_be_copied__c for SObject Name at line 18 column 26.

Please help .. TIA..    
 
Best Answer chosen by lalit kumar 11
Magesh Mani YadavMagesh Mani Yadav
Hi Lalit,

If you want to copy some field value or assigning some default value to the inserted/updated record always use BEFORE Trigger.
Here I have updated your trigger. Try this should work for you.
trigger leadtasktrigger on Task (before insert) {
	Map<Id,Task> mapleadTskIds = new Map<Id,Task>();
    for(task ta :trigger.new){
        if(ta.whoid!=null && String.valueof(ta.whoid).startsWith('OOQ')){
            mapleadTskIds.put(ta.whoid,ta);
        }        
    }
    for(Lead l : [select id,value_to_be_copied__c from Lead where id in:mapleadTskIds.keySet()]){
        if(mapleadTskIds.get(l.Id)!=null){
          mapleadTskIds.get(l.Id).copied_value__c = l.value_to_be_copied__c;  
        }
    }
}

 

All Answers

Magesh Mani YadavMagesh Mani Yadav
Hi Lalit,

If you want to copy some field value or assigning some default value to the inserted/updated record always use BEFORE Trigger.
Here I have updated your trigger. Try this should work for you.
trigger leadtasktrigger on Task (before insert) {
	Map<Id,Task> mapleadTskIds = new Map<Id,Task>();
    for(task ta :trigger.new){
        if(ta.whoid!=null && String.valueof(ta.whoid).startsWith('OOQ')){
            mapleadTskIds.put(ta.whoid,ta);
        }        
    }
    for(Lead l : [select id,value_to_be_copied__c from Lead where id in:mapleadTskIds.keySet()]){
        if(mapleadTskIds.get(l.Id)!=null){
          mapleadTskIds.get(l.Id).copied_value__c = l.value_to_be_copied__c;  
        }
    }
}

 
This was selected as the best answer
lalit kumar 11lalit kumar 11
Thanks Magesh..