Skip to main content
Hello, 

I am trying to create a trigger on the Task object that will allow me to pull the MobilePhone value from the associated Lead or Contact and populate a custom field on the Task.  I am using this article as a reference: citing source article

Here is the trigger as I currently have it in my sandbox:

 

Trigger TaskBefore on Task(before insert, before update){

Map<Id, List<Task>> whoIds = new Map<Id, List<Task>>{};

For (Task t : trigger.new)

If(t.WhoId != null){

List<Task> tasks = whoIds.get(t.WhoId); //this should be t.WhoId (not task.WhoId)

If (tasks == null){

tasks = new List<Task>{};

whoIds.put(t.WhoId, tasks);

}

tasks.add(t);

}

For (Lead ld : [Select Id, Name, MobilePhone where Id in :whoIDs.keySet()])

For(Task t : whoIds.get(ld.id))

t.Mobile__c = ld.Mobile;

For(Contact con : [Select Id, Name, MobilePhone where Id in :whoIds.keySet()])

For(Task t : whoIds.get(con.id))

t.Mobile__c = con.Mobile;

}

However it is yielding the error: Error: Compile Error: unexpected token: where at line 14 column 49

Have I misunderstood the syntax here?  I would really appreciate some help!

Thanks, 

John
8 Antworten
  1. 9. März 2016, 13:47
    Hi John,

    The reason for the issue is you are missing "from" in the query

    write query like this

    For (Lead ld : [Select Id, Name, MobilePhone From Lead where Id in :whoIDs.keySet()]

    so that that issue will go.

    one more thing you need to take here is

    07            If (tasks == null){

    08            tasks = new List<Task>{};

    09            whoIds.put(t.WhoId, tasks);

    10        }

    I belive it should be like below otherwise that list will have only one record all the time and will not work for bulk update

    07            If (tasks == null){

    08            tasks = new List<Task>();

    09            }

    10            whoIds.put(t.WhoId, tasks);

    let me know, if it helps you or need help :)

    shiva.sfdc.backup@gmail.com
0/9000