+ Start a Discussion
Lauren HonyotskiLauren Honyotski 

Trigger on Task to query WhoID and populate custom field

I need a trigger to query the WhoID on task, and if it is lead, look at lead field pi__score__c and populate this on task field score__c. The below trigger is not working in Sandbox. This is a modified trigger, in case certain parts don't make sense Any ideas?


trigger UpdatedRelatedLead on Task (after insert, after update) {

// Goal: Find the Lead Score of the lead that a task is related to
// and update the Score__c field on the task object with the value

// If related to a Lead, query to find out the LeadID

// Create collection of tasks that are related to a lead (where the lead is listed only once)
    Set<Id> LeadIds = new Set<Id>();
    for(Task t : trigger.new){
        String wId = t.WhatId;
        if(wId!=null && wId.startsWith('00Q') && !LeadIds.contains(t.WhoId)){
            LeadIds.add(t.WhoId);
        }
    }
    // Pull in Lead ids and related field to populate task record
    List<Lead> taskLeads = [Select Id, pi__score__c from Lead where Id in :LeadIds];
    Map<Id, Lead> ldMap = new Map<Id, Lead>();
    for(Lead l : taskLeads){
        ldMap.put(l.Id,l);
    }
    // Update custom task field with custom lead field
    for(Task t : trigger.new){
        String wId = t.WhoId;
        if(wId!=null && wId.startswith('00Q')){
            Lead thisLead = ldMap.get(t.WhoId);
            if(thisLead!=null){t.Score__c = thisLead.pi__score__c;}
        }
    }
}
Balaji Chowdary GarapatiBalaji Chowdary Garapati
@Lauren Honyotski:

 Change the trigger events to before instead of after.,

trigger UpdatedRelatedLead on Task (before insert, before update) {

For more info on trigger events and trigger context variables please refer to below link:

https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers.htm
https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers_context_variables.htm

Hope it helps.,

Thanks,
Balaji
Lauren HonyotskiLauren Honyotski
Thanks Balaji. That did not update the score...
Balaji Chowdary GarapatiBalaji Chowdary Garapati
can you add debug log and create task again just to see whats going on and share it here?

Thanks,
Balaji
Lauren HonyotskiLauren Honyotski

33.0 APEX_CODE,DEBUG;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WORKFLOW,INFO 12:24:57.074 (74151424)|EXECUTION_STARTED 12:24:57.074 (74182145)|CODE_UNIT_STARTED|[EXTERNAL]|TRIGGERS 12:24:57.074 (74215132)|CODE_UNIT_STARTED|[EXTERNAL]|01qn00000004Hl7|UpdatedRelatedLead on Task trigger event BeforeInsert for [new] 12:24:57.075 (75142944)|SYSTEM_CONSTRUCTOR_ENTRY|[9]|<init>(Integer) 12:24:57.075 (75180864)|SYSTEM_CONSTRUCTOR_EXIT|[9]|<init>(Integer) 12:24:57.075 (75395934)|SYSTEM_METHOD_ENTRY|[10]|List<Task>.iterator() 12:24:57.075 (75551691)|SYSTEM_METHOD_EXIT|[10]|List<Task>.iterator() 12:24:57.075 (75577671)|SYSTEM_METHOD_ENTRY|[10]|system.ListIterator.hasNext() 12:24:57.075 (75605582)|SYSTEM_METHOD_EXIT|[10]|system.ListIterator.hasNext() 12:24:57.075 (75848388)|SYSTEM_METHOD_ENTRY|[10]|system.ListIterator.hasNext() 12:24:57.075 (75868409)|SYSTEM_METHOD_EXIT|[10]|system.ListIterator.hasNext() 12:24:57.076 (76437898)|SOQL_EXECUTE_BEGIN|[17]|Aggregations:0|SELECT Id, pi__score__c FROM Lead WHERE Id = :tmpVar1 12:24:57.080 (80622403)|SOQL_EXECUTE_END|[17]|Rows:0 12:24:57.080 (80746570)|SYSTEM_METHOD_ENTRY|[19]|List<Lead>.iterator() 12:24:57.080 (80820211)|SYSTEM_METHOD_EXIT|[19]|List<Lead>.iterator() 12:24:57.080 (80838772)|SYSTEM_METHOD_ENTRY|[19]|system.ListIterator.hasNext() 12:24:57.080 (80853547)|SYSTEM_METHOD_EXIT|[19]|system.ListIterator.hasNext() 12:24:57.080 (80872973)|SYSTEM_METHOD_ENTRY|[23]|List<Task>.iterator() 12:24:57.080 (80890263)|SYSTEM_METHOD_EXIT|[23]|List<Task>.iterator() 12:24:57.080 (80898698)|SYSTEM_METHOD_ENTRY|[23]|system.ListIterator.hasNext() 12:24:57.080 (80911251)|SYSTEM_METHOD_EXIT|[23]|system.ListIterator.hasNext() 12:24:57.081 (81023199)|SYSTEM_METHOD_ENTRY|[25]|String.startsWith(String) 12:24:57.081 (81052799)|SYSTEM_METHOD_EXIT|[25]|String.startsWith(String) 12:24:57.081 (81089205)|SYSTEM_METHOD_ENTRY|[26]|Map<Id,Lead>.get(Object) 12:24:57.081 (81113415)|SYSTEM_METHOD_EXIT|[26]|Map<Id,Lead>.get(Object) 12:24:57.081 (81123555)|SYSTEM_METHOD_ENTRY|[23]|system.ListIterator.hasNext() 12:24:57.081 (81135677)|SYSTEM_METHOD_EXIT|[23]|system.ListIterator.hasNext() 12:24:57.843 (81151213)|CUMULATIVE_LIMIT_USAGE 12:24:57.843|LIMIT_USAGE_FOR_NS|(default)| Number of SOQL queries: 1 out of 100 Number of query rows: 0 out of 50000 Number of SOSL queries: 0 out of 20 Number of DML statements: 0 out of 150 Number of DML rows: 0 out of 10000 Maximum CPU time: 0 out of 10000 Maximum heap size: 0 out of 6000000 Number of callouts: 0 out of 100 Number of Email Invocations: 0 out of 10 Number of future calls: 0 out of 50 Number of queueable jobs added to the queue: 0 out of 50 Number of Mobile Apex push calls: 0 out of 10 12:24:57.843|CUMULATIVE_LIMIT_USAGE_END 12:24:57.081 (81203764)|CODE_UNIT_FINISHED|UpdatedRelatedLead on Task trigger event BeforeInsert for [new] 12:24:57.265 (265420561)|ENTERING_MANAGED_PKG|rh2 12:24:57.352 (352589296)|CODE_UNIT_STARTED|[EXTERNAL]|Workflow:Task 12:24:57.353 (353119513)|CODE_UNIT_FINISHED|Workflow:Task 12:24:57.369 (369479821)|CODE_UNIT_FINISHED|TRIGGERS 12:24:57.369 (369492802)|EXECUTION_FINISHED
Bhanu MaheshBhanu Mahesh
Hi Lauren,

You are considering WhatId instead of Who Id

String wId = t.WhatId;

Change it to

String wId = t.WhoId;

Regards,
Bhanu Mahesh
Lauren HonyotskiLauren Honyotski
Thanks Bhanu! That worked. 
Lauren HonyotskiLauren Honyotski
Some help with the test class would also be greatly appreciated: Here's what I have

public class testscoreontask {

{

    
    Account[] acc = new account[]
    {
        new account(lastname = 'test')
    };
    insert acc;
    
    // insert contact
    Contact[] cont = new Contact[]
    {
        new Contact(LastName = 'testcontact1'),
        new Contact(LastName = 'testcontact2'),
        new Contact(Account = 'test'),
        new Contact(pi__score__c = '135')
    };    
    insert cont;    
    // insert task
    
    Task[] tas = new task[]
    {
        new task(subject = 'subject')
    };
    insert tas;
    
    
    Test.StartTest();
    Test.StopTest();
    
}
}
Bhanu MaheshBhanu Mahesh
Hi Lauren,

Insert Lead record and assign that Lad record Id to WhoId while inserting task.
Then your trigger will be covered.

Now your are inserting Account and contacts. Instead of that insert Lead.
And while creating task 
new task(subject = 'subject', WhoId = lead.Id)

Regards,
Bhanu Mahesh