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
Beth StrellaBeth Strella 

I want to create a field that has the last time the account was touched that was not done by a system admin.

I want to create a field that has the last time the account was touched that was not done by a system admin.
By touch, I mean Newest date out of the following: Task Created date, Event created to date, task modified date, event modified date.
This trigger was built for us but doesn’t exclude the System admin as well as doesn’t give us the last day if they created an activity just gives us a modified date.  Not really sure how to fix it/ adjust it. Any help is appreciated. I have never used Apex before.
 
trigger UpdateLastActivityDate4318 on Task (after insert, after update, after delete, after undelete)
{
    Set<Id> accountIds = new Set<id>();
    if(Trigger.isDelete)
    {
        //always get the old for deleted activities
        for(Task t : Trigger.old)
        {
            accountIds.add(t.AccountId);
        }
    }
    else   
    {
        for(Task t : Trigger.new)
        {           
            accountIds.add(t.AccountId);
        }                  
       
    }
   
  List<Account> accounts =           
            [
                SELECT
                  Id,
                    (
                        SELECT
                          Subject,
                            LastModifiedDate
                        FROM Tasks
                        ORDER BY isClosed ASC, LastModifiedDate DESC
                        LIMIT 1
                    ),
                  Last_Activity_Date__c
                FROM Account
                WHERE Id in :accountIds
            ];
   
   
   
    for (Account a : accounts)
    {
   
        system.debug(a);
       
       
        if(a.Tasks.size() > 0)
        {
            system.debug(a.Tasks[0]);
            a.Last_Activity_Date__c = a.Tasks[0].LastModifiedDate;
        }
        else
        {
            a.Last_Activity_Date__c = null;
        }
    }
   
    update accounts;
}
 
}
 
 
 
Jay Parikh 36Jay Parikh 36
Hi -- Here is a solution using formul field it is easy .
First on account you have to create new formula field :
Formula field which give you last modified profile name  :::::    LastModifiedBy.Profile.Name  (Which give you last modified user profile name )

after you will create new formula field name :::Last modified date 
formula is ::

IF(Last_Prodile_Name__c <> 'System Administrator', 
DATEVALUE(LastModifiedDate ) , 
null )

Let me know if you need anyhelp or mark this answer as a best answer!!!
Beth StrellaBeth Strella
Hi Jay Parikh 36- It sort of works but not 100% there yet. If a user updates the account it works. But then if the system admin does an update to that account it removed the last modified date field since we have null in the field. I would need the last modified to stay. available and basically ignore that the admin touched the account. User-added image

but shoudl return 4/4/2018 since Dan Nikolich created an activity todayUser-added image
Jay Parikh 36Jay Parikh 36
You will create workflow rule where you need to create new date field and 
condition to update that date field is  ::: Last_Prodile_Name__c <> 'System Administrator' it will update with last modified date   DATEVALUE(LastModifiedDate )  


if it Last_Prodile_Name__c == 'System Administrator' then put  PRIORVALUE( New Field ) )
Beth StrellaBeth Strella
when i put the new field name in I get this error Error: Formula cannot use another formula field that directly or indirectly refers to itself. Account.Last_modified_date_4_4_18__c -> Account.Last_modified_date_4_4_18__c if i change dthe formula instead to this:

IF(LastModifiedBy.Profile.Name <> 'System Administrator', 
DATEVALUE(LastModifiedDate ) , 
PRIORVALUE(LastModifiedDate))

I get this error Error: Function PRIORVALUE may not be used in this type of formula

I have used a prior value before in an if statement so not sure why it doesn't allow it this time.