2

When changing owner for an Account opens a pop up that says,

The new owner will also become the owner of these records related to Think Big Corp that are owned by you.
 1. Open opportunities
 2. Contracts in Draft and In Approval status
 3. Orders in Draft status (with or without a transferring contract)
 4. Contacts
 5. Notes and attachments
 6. Open activities

If I change the owner of opportunities directly, it triggers the code I have written in Opportunity trigger. But when I change owner for Account it simply changes the owner of the opportunity without triggering the opportunity's trigger.

The same happens for Contacts too. Contact has a trigger code which updates the Individual's owner to the same owner of the associated Contact. But when I change owner for Account it simply changes the owner of the Contact but doesn't change the associated Indiviudal's owner to that of the new owner of Contact.

Is there a workaround without changing too much code?

1 Answer 1

1

That's how Salesforce operates, for better or for worse. All you can do is write a Queueable/future method to manually call those triggers after the transaction completes. Something like:

Account[] needsPostProcessing = new Account[0];
for(Integer i = 0, s = Trigger.new.size(); i < s; i++ {
  if(Trigger.new[i].OwnerId != Trigger.old[i].OwnerId) {
    needsPostProcessing.add(Trigger.new[i]):
  }
}
if(needsPostProcessing.size() > 0) {
  System.enqueueJob(new AccountPostProcessing(needsPostProcessing));
}

Your other class would look like:

public class AccountPostProcessing implements Queueable {
  public AccountPostProcessing(Account[] needsUpdates) {
    scope = needsUpdates;
  }
  Account[] scope;
  public void execute(QueueableContext context) {
    update [select id from contact where AccountId = :scope];
    update [select id from opportunity where AccountId = :scope];
  }
}
5
  • Thanks a lot, for contacts I think your idea will work, but for opportunities it is bit complicated, as I am aggregating the roles of owners of opportunity on to a field in the associated Account, for this I am checking if the old opp owner role != new opp owner role, unfortunately by the time of the queueable execution, the old owner role will be the same as the new owner role and therefore the logic will not execute. But for now I have a different way for opportunities.
    – nSv23
    Commented Sep 10, 2020 at 15:09
  • Also is there a way to capture "Transfer account owner's closed opportunities" checkbox value? I want to know if the user has selected this option when transfering accounts, based on which I can modify my code further.
    – nSv23
    Commented Sep 10, 2020 at 15:11
  • 1
    @nSv23 No, there's no way to tell if this was selected. Using queueable, you could also query the opportunities and see if they changed, though.
    – sfdcfox
    Commented Sep 10, 2020 at 15:13
  • ok. If I understand correctly queueable executes after the Account's change owner, which also changes Opportunity owner right? Querying in the queueable will give the changed value am I correct? So the owner of the opportunity has already changed by the time queueable executes. correct?
    – nSv23
    Commented Sep 10, 2020 at 15:16
  • 1
    @nSv23 Correct, in the queueable, the owner will already be the new owner. You can therefore check to see which records changed by comparing the values you queried from the trigger and the values queried from the Queueable.
    – sfdcfox
    Commented Sep 10, 2020 at 15:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .