I am writing a batch class on the UserTerritory2Association SObject and I need to perform an upsert operation. I have tried to use insert and records are getting created but if I need to do upsert I am not sure what to use as ID based on which a record will be inserted or updated. I also tried to search for any documents on the internet but didn't get anything useful.

1 Answer 1


No, it's not possible. As the documentation for UserTerritory2Association states:

Supported Calls

create(), delete(), describeSObjects(), query(), retrieve()

As such, you can only create() and delete() these associations, and you cannot update() or upsert() them.

Probably the best way to handle this is to simply use allOrNone=false to ignore any errors:

Database.insert(newTerritoryAssignments, false);
  • So what should I do if a record needs to be updated?
    – Night Guy
    Apr 6, 2021 at 13:25
  • @NightGuy you'd have to delete and reinsert.
    – sfdcfox
    Apr 6, 2021 at 13:44
  • Consider there are records like: User1 --> Teritorry1 and User2 --> Teritorry1. Now what if I need to update Teritorry1 to Teritorry3 for User1 then what should I do? How will my code identify that specific record? Do consider that I might need to make N number of similar update.
    – Night Guy
    Apr 6, 2021 at 14:01
  • @NightGuy You would delete the old record, and insert the new. Don't think of it as a single operation (they are moving from Territory1 to Territory3), but as two separate actions (they are leaving Territory1 and joining Territory3).
    – sfdcfox
    Apr 6, 2021 at 14:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.