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
Nick Nelson 9Nick Nelson 9 

Apex - LastModifiedById, OldMap, NewMap, all return same value

I'm trying to write a 'before update' apex trigger that uses values from the oldmap of an object, but have been having issues. I have been using a text field on the object to test the LastModified values, but found that the new value, the oldmap value, and the newmap value are identical. 
The code below shows what I'm talking about:
trigger Example on Contact (before update) {
    for (Object o: trigger.new) {
        try {
            o.Description = 'Current LastModifiedBy: ' + o.LastModifiedById + ' \noldMap: ' + Trigger.OldMap.get(o.Id).LastModifiedById + ' \nnewMap: ' + trigger.newMap.get(o.Id).LastModifiedById;
        } catch (Exception e) {}
    }
}
Again: o.LastModifiedById, Trigger.OldMap.get(o.Id).LastModifiedById, and Trigger.NewMap.get(o.Id).LastModifiedById, all return the same value.

Any help would be appreciated, thanks.
 
Nick Nelson 9Nick Nelson 9
Sorry, should have clarified. The cases where this would be triggered is if a user updates a record from SalesForce, and when a user updates a record externally through SAP. The trigger is supposed to detect when the record was modifed by SAP, and if so, change a specific field. The LastModifiedById field is updated correctly based on whether it was updated via SF or SAP, but the issue is with the NewMap and OldMap being identical.
Egor Gerasimenko 9Egor Gerasimenko 9

Per https://salesforce.stackexchange.com/questions/120795/why-lastmodifiedbyid-in-trigger-new-gives-old-value

"System audit fields like LastModifiedById, LastModifiedDate, and SystemModStamp are not updated until later in the transaction. You shouldn't rely on any of those values being set, especially in a "before" trigger"

Nick Nelson 9Nick Nelson 9
How should I go about this then? I'm unable to do 'after update' as it says it is Read Only.
Egor Gerasimenko 9Egor Gerasimenko 9
Well that depends on what you are trying to do. As mentioned in the stackexchange post, you can figure out the user that's modifying the record (and is essentially the "new" LastModifiedBy) by calling 
 
UserInfo.getUserId()

Also, you can still modify records in after triggers, you just have query them with a SOQL statement first.
Nick Nelson 9Nick Nelson 9
In this kind of situation, relative to the object what would I apply getUserId() to? ______.getUserId()?
Egor Gerasimenko 9Egor Gerasimenko 9

I'm not sure what you are asking. Presumably you are trying to find out who modified the record previously and who's modifying it now.

 

trigger Example on Contact (before update) {
    for (Object o: trigger.new) {
            o.Description = 'Was previously modified by: ' + Trigger.OldMap.get(o.Id).LastModifiedById + ' but is now modified by ' + UserInfo.getUserId();
    }
}
Nick Nelson 9Nick Nelson 9
UserInfo.getUserId() gets it done, thanks a bunch!