0

I have a trigger where I'm trying to make it work like the following: When a Activity record is inserted by a user that matches the value in a user lookup field on Accounts named "Customer Success Manager", the created date of the activity goes in the CE_Last_Activity__c field on Accounts. I keep getting the following error: Error: Compile Error: line 21:13 no viable alternative at character '$' at line 21 column 13". The line starts with if(!$User.Name

Here is what I have so far:

trigger CELastActivity on Task (after insert) {

datetime myDate = datetime.now();
List<Account> accToUpdate = [SELECT Id,CE_Last_Activity__c FROM Account WHERE Customer_Success_Manager__c != Null];
Map<Id, Task> taskMap = new Map<Id, Task>();

for (Task t: Trigger.New){
     if (t.WhatId  != null ){
        taskMap.put(t.WhatId, t);
    }
}

system.debug('taskMap = '+taskMap);
   if (taskMap.size() > 0)
{
          accToUpdate = [SELECT Id, CE_Last_Activity__c
                    FROM Account 
                    WHERE Id IN: taskMap.keySet()];

    for (Account a: accToUpdate){
    if (!$User.Name = a.Customer_Success_Manager){
         a.CE_Last_Activity__c = myDate;
                            }

    system.debug('accToUpdate = '+accToUpdate);
    if (accToUpdate.size() > 0)
    {
        update accToUpdate;
     }
  }
}
2
  • Is there a particular issue you're running into? What is your actual question?
    – OBerm
    Commented Mar 17, 2015 at 20:59
  • @Dupe_jockey sorry I can't believe I didn't state the issue! I get a compile error at the $ in column 21 which starts with "if (!$User.Name. The error says "Compile Error: line 21:13 no viable alternative at character '$' at line 21 column 13" Commented Mar 17, 2015 at 21:02

1 Answer 1

0

If the Account field is a lookup, you will want to change line 21 to read:

If(Userinfo.getUserId() == a.Customer_Success_Manager__c){

That should solve the error you are getting.

The $User global variable is not available in Apex.

Also, you are updating each account inside the for loop. This is not a good practice. You should bulkify your code by creating a list of accounts to update and then updating them all at once after the loop.

Here is the updated code:

trigger CELastActivity on Task (after insert){

    List<Account> accToUpdate = new List<Account>();
    Map<Id, Task> taskMap = new Map<Id, Task>();

    for (Task t: Trigger.New){
         if (t.WhatId  != null ){
            taskMap.put(t.WhatId, t);
        }
    }

    system.debug('taskMap = '+taskMap);
    if (taskMap.size() > 0)
    {
        for (Account a: [SELECT Id, CE_Last_Activity__c
                        FROM Account 
                        WHERE Id IN: taskMap.keySet()])
        {
            if (UserInfo.getUserID() == a.Customer_Success_Manager__c)
            {
                 a.CE_Last_Activity__c = taskMap.get(a.Id).CreatedDate;
                 accToUpdate.add(a);
            }
        }

        system.debug('accToUpdate = '+accToUpdate);
        if (accToUpdate.size() > 0)
        {
            update accToUpdate;
        }
    }
}

Good luck!

Luis Luciani

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .