Skip to main content

How can I build a rollup or automation to populate the Account Owner field based on a role of a user in the 'Users in Assigned Territories' related list? I've taken a look at the ETM v2 ERD but can't find between that and the objects relationship fields on the "UserTerritory2Association" how to relate the user to the account. Is it indirectly associated with the account through another object in the ERD? It's unclear the path to build the referencing between this related list and the Account Owner field on the Account object. Thanks!

4 respuestas
  1. 5 dic 2022, 22:10

    I have done this before - it's been a few years. Even with code this is difficult. 

     

    "SObject type does not allow triggers: ObjectTerritory2Association"

     

    So you can't trigger on an Account being associated with a Territory. I found that in the Account trigger Before Insert/Before Update context where you could change OwnerId, a newly assigned territory isn't there yet. 

     

    What you can do is in the Account trigger handler's After Insert and After Update context, query for the relevant relevant ObjectTerritory2Association records and do something with that information. But you want to update that Account itself, which you can't do from within that After context. So... async Apex for the win? 

     

    I called a queueable or future method (I don't recall which) but only when territories had changed. In the Account trigger handler before update context, I queried for and stored the current territories in a static list; In the after context, I queried and compared, then called the async method to change user lookup fields (like owner) on the account. 

     

    Alternatively you could just have this run as scheduled Apex that looks over all recent changes to ObjectTerritory2Association and updates Accounts owner/lookup fields accordingly. 

0/9000