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
JustDJustD 

Setting the Territory on a new Opportunity created in APEX

When I create a new Opportunity in Apex I would like to set the Territory for that Opportunity using the same logic as is applied when an Opportunity is created manually in the normal user interface.

 

In other words: I'd like to create and insert a new Opportunity with an Account and Owner in APEX and have its territory set exactly as Salesforce does when a user create their own Opportunities according to these rules (without having write the detailed logic to emulate this myself).  Is there any method/feature/ability in APEX/Salesforce to achieve this?

 

  • If there is a single territory that both the account and the opportunity owner belong to, the opportunity is assigned to that territory.
  • If more than one territory is common to both the account and the opportunity owner, the opportunity is not automatically assigned to any territory. The opportunity owner must manually assign a territory to the opportunity.
  • If no territory is common to both the account and the opportunity owner, the opportunity owner cannot assign the opportunity to a territory.
Thanks
Derek