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
Paula Jarvis 4Paula Jarvis 4 

Apex Code Help on Custom Date Field on Account

I created a custom date field on the Account called Account_Last_Activity__c and I want to update it based on the last activity by specific Users. I then want to run a report based on the Last Activtiy and by whom. I got this far on my apex code and received an "unexpected toke" error. I have not been able to include the specific User IDs in the code either. Any help would be appreciated. Thank you!


rigger UpdateAccountLastActivityAccount on Task (after insert, after update) {
    Set<String> whatIds = new Set<String>();
    
    for (Task t : Trigger.new) {
        whatIds.add(t.WhatId);
    }
    
    List<Account> acct = [SELECT Id, Account_Last_Activity__c FROM Account WHERE Id =: whatIds];
    
    Map<String, Task> taskMap = new Map<String, Task>();
    
                }
        }   
    }
         
    for (Account a : acct) {
        if (taskMap.containsKey(a.Id)) {
            a.Account_Last_Activity__c = taskMap.get(a.Id).ActivityDate;
        }
    }
    update acct;
}
Azhar Aziz GAzhar Aziz G
Trigger UpdateAccountLastActivityAccount on Task (after insert, after update) {
    Set<ID> whatIds = new Set<ID>();
    
    for (Task t : Trigger.new) {
        whatIds.add(t.WhatId);
    }
    List<Account> acct = [SELECT Id, Account_Last_Activity__c FROM Account WHERE Id in :whatIds];
    
	if( !acct.isEmpty() ){
		Map<ID, Task> taskMap = new Map<ID, Task>();
		for( Task t : Trigger.new ){
			taskMap.put( t.WhatId , t );
		}
			  
		for (Account a : acct) {
			if (taskMap.containsKey(a.Id)) {
				a.Account_Last_Activity__c = taskMap.get(a.Id).ActivityDate;
			}
		}
		update acct;
	}
}
Try Above code...
Paula Jarvis 4Paula Jarvis 4
Hello! Thank you for responding so quickly. I tried the code and got another error message = [image: Error] *Error: Compile Error: Incorrect SObject type: Task should be Account at line 1 column 1* Does this code in corporate User IDs for specific Users? I have about 8 Users that I want to identify for the Last Activity to record. Paula J Jarvis Salesforce Administrator E pjarvis@phreesia.com D (646) 582-9154