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
Trevor ViallTrevor Viall 

How to I check who updated records

Within Apex is there a way that I can check who updated a record. Basically a to check see if someone other than the account holder updated a record. 
Best Answer chosen by Trevor Viall
mukesh guptamukesh gupta
Hi Trevor,

you can use below code:-
trigger TriggerName on Account (before update) {
if (trigger.isUpdate) {
  for( Account a : trigger.New) {
      if(a.Owner <> a.LastModifiedBy) {
        System.Debug('Modified by another -->>>> '+a.LastModifiedBy);
      } else {
        System.Debug('Modified by Account owner'+a.Owner');
      }
  }
}

If this solution is usefull for you, Please mark as a Best Answer to help others.



Regards
Mukesh
 

All Answers

Andrew GAndrew G
LastModifiedBy field
 
trigger ExampleTrigger on Account (before update) {
if (trigger.isUpdate) {
  for( Account a : trigger.New) {
      if(a.Owner <> a.LastModifiedBy) {
        System.Debug('none owner update');
      } else {
        System.Debug('owner update');
      }
  }
}

regards
andrew

 
Kanav TechfineryKanav Techfinery

Hi Trevor,

This can be done as mentioned by Andrew in the previous comment. However, is there some bigger logic that you're trying to implement? 

Or is it only to check if 'someone other than the account holder updated a record'? If it is the latter, then why not go for a declarative approach? 

mukesh guptamukesh gupta
Hi Trevor,

you can use below code:-
trigger TriggerName on Account (before update) {
if (trigger.isUpdate) {
  for( Account a : trigger.New) {
      if(a.Owner <> a.LastModifiedBy) {
        System.Debug('Modified by another -->>>> '+a.LastModifiedBy);
      } else {
        System.Debug('Modified by Account owner'+a.Owner');
      }
  }
}

If this solution is usefull for you, Please mark as a Best Answer to help others.



Regards
Mukesh
 
This was selected as the best answer