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
Rahul_SahaRahul_Saha 

Changing User when a field has been updated by apex code

Hi,

 

Whenever I use apex callout to update some field or even any apex trigger for that matter, the record history says that the field has been updated by the logged in user. This makes it sometime confusing to keep track on which fields the logged in users are actually updating . I understand that the apex code have run due to some actions by the logged in user but still is there any way to show in record history that a particular field is updated by apex .

 

Thanks,

Rahul

Best Answer chosen by Admin (Salesforce Developers) 
Jeff MayJeff May
You can't change the user context since you are still operating in the context of the current user.

All Answers

Jeff MayJeff May

You are right that changes caused by triggers will always show as being made from the user who caused the trigger to fire in the UI.  There is no way to know for sure whether a particular field was changed in the UI or by a trigger, since trigger changes on their objects are part of the same "change set" as the UI changes.

 

If a trigger is updating a different record, then there is a way to know.   If you add a custom field to the object, say 'date record changed by trigger', and 'track changes' on the field, then only set it during trigger execution.  You'll be able to see those changes in the record change history.

 

For example, if you have a Contact trigger, that updates an Account record when something changes on the Contact, you can add a 'date changed by trigger' to Account.  In the Contact trigger, add Account.date_changed_by_trigger__c = Date.Today().

 

This will not work if the trigger only updates the object being edited in the UI, since all the field changes will be tracked together.

Rahul_SahaRahul_Saha

Thanks Jeff .

 

Can we do this if some webservice callout from some custom button on object pagelayout is updating fields? I mean, the button on click  updates the same record (single object.)

Jeff MayJeff May

In that case I think you'll be OK since there are no other UI-driven field changes being made at the same time.  The user chose to click the button, and may or may not make other field changes, but that's OK. Unlike an inline edit where several fields could be changed, and then a trigger changes one more.

Rahul_SahaRahul_Saha

So while making a callout can we change the user context ? I mean, if anything gets updated due to callout results that can be shown in record history on behalf of a generic user (say Integration). This can help us.

I found that system.runas() can only work for test classes.

Jeff MayJeff May
You can't change the user context since you are still operating in the context of the current user.
This was selected as the best answer
Rahul_SahaRahul_Saha

Thanks for clarification

SFDC-ASSFDC-AS
Hi Rahul, 

Its a long time since you posted this..but if you remember, have you found any solution for this --> " If anything gets updated due to callout results that can be shown in record history on behalf of a generic user (say Integration)"