+ Start a Discussion
Nerv123Nerv123 

"Change Owner" in Opportunity to include "Change Territory"

I need to have Territories assigned to the Opportunity based on the owner of the opportunity.
If the Opportunity Owner is changed using the "Change" option beside the field Owner in Opportunity, then the logic must include automatically changing the territory too, based on the territory assignment of the owner.
Where can I find the standard salesforce logic for "Change"?
If I cannot find it, any ideas on how I can achieve this complete logic with a single click?
 
Thanks So Much!
werewolfwerewolf
That sounds like something you'd want to do with an Apex trigger, actually.  It would likely be much easier.
rpmcguirerpmcguire

Werewolf,

I'm a newbie to Apex and I tried to create a trigger (below) to do exactly that, but can't seem to get it to work properly. Any suggestions would be greatly appreciated. Thanks!

Rich

 



 

trigger setOppTerritory on Opportunity ( before insert, before update ) { for (Opportunity o : [ SELECT Id, TerritoryId from Opportunity where Id in : Trigger.new ] ) { List <Opportunity> opp = new List<Opportunity>(); opp.TerritoryId = opp.owner.TerritoryId; update opp; } }

 


 

werewolfwerewolf
Your problem is that your select doesn't actually include opp.owner.    You have to amend it to get the territory ID for the owner ID (which I think is in a separate table so you'll have to query it).