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
John NeffJohn Neff 

Task Trigger Error: unexpected token

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
Best Answer chosen by John Neff
ShivaKrishna(Freelancer)ShivaKrishna(Freelancer)
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

All Answers

ShivaKrishna(Freelancer)ShivaKrishna(Freelancer)
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
This was selected as the best answer
John NeffJohn Neff
Ahh Thank you Shiva I can't believe I missed that!

John
ShivaKrishna(Freelancer)ShivaKrishna(Freelancer)
Hi John

can you please vote for my reply as best answer, if it helped you.

shiva.sfdc.backup@gmail.com
 
John NeffJohn Neff
My pleasure!  I meant to before but for some reason it didn't take!

Thanks, 

John
Richard Jimenez 9Richard Jimenez 9
Hi John,

By having the trigger on the task, if the mobile phone is changed on the Lead or Contact then the task will not be in-sync.

You could do this without any code by creating two process builder processes, one on Lead and one on Contact, that update the related tasks when the mobile phone is changed?

Hope that helps,
Richard
John NeffJohn Neff
Hi Richard, 

That was my first instint, but process builder does not seem to get along nicely with the dataloader - and I was trying to create a solution that wouldn't cause issues when bulk updating records. 

My next step was to see if I could install an "after update" trigger on the lead and contact objects that would update any related tasks. 

Is there a way that I can make process builder processes bulk safe?

John
Richard Jimenez 9Richard Jimenez 9
Hi John,

There was a bulkification update to process builder in Winter16 (see https://success.salesforce.com/ideaView?id=08730000000DhBlAAK). Having said that (as Im not 100% if you will have any other issues), it shouldnt take long to create this and run a test update to validate.

To make the update more efficient i.e. only update the tasks that you really need to, think about using criteria in the update record to only update where the moble phones dont match, and maybe where the task is not complete?

Thanks,
Richard.
John NeffJohn Neff
Thanks Richard, I'll give that a try!