H. L. M.
H. L. M.

Reputation: 1

How do I create a trigger so that an event is created every time the owner id changes for an account?

As above. In salesforce, I would like to create a trigger so that an event is created when the owner id changes for an account. This is so that the changes in owner id can be tracked in the activity timeline of the account. I have searched for solutions but did not find anything.

Would be great if someone can help me! (or if I've missed a post, to point me in the right direction.)

Thanks!

Upvotes: 0

Views: 113

Answers (1)

XaxD
XaxD

Reputation: 1538

You would create a trigger that implements the onUpdate event and write an associated class to execute your Apex code.

AccountUpdate.trigger

trigger AccountUpdateTrigger on Account (after update) {

if  (!TriggerUtil.ExecutingTriggers.containsKey('AccountUpdateTrigger')){
    Set<Id> processedIds = new Set<Id>();
    TriggerUtil.ExecutingTriggers.put('AccountUpdateTrigger',processedIds);
}   

if(trigger.isAfter && trigger.isUpdate) {
    AccountUpdateTriggerHandler.OnAfterUpdate(trigger.newMap, trigger.oldMap);
}
}

Then in your AccountUpdateTriggerHandler class you would write a handler

public Class DocumentRequestTriggerHandler {

public static void onAfterUpdate(Map<Id, Account> newMap, Map<Id, Account> oldMap){
    updateTracking(newMap,oldMap);
}

public static void updateTracking(Map<Id, Account> newMap, Map<Id, Account> oldMap){
    // your necessary code here 
}
}

Alternatively, for your use cse you could also just turn on History Tracking at the field level.

Upvotes: 1

Related Questions