+ Start a Discussion
Becky Hammond 6Becky Hammond 6 

OppTerrAssignDefaultLogicFilter Help needed

Hi All - I'm hoping someone can help.  I've deployed the class at the following link:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_interface_TerritoryMgmt_OpportunityTerritory2AssignmentFilter.htm

I'm using this to assign opportunities.  All of our accounts have exactly one opportunity.  When I run the opportunity filter, it is assigning 165 out of 629 opportunities.  If I run it again, it assigns 464 opportunities and leaves 165 unassigned.  Has anyone experienced this?  Salesforce support told me my only option is to look for help here as we are not a premier customer.  

Thanks for any help you can provide.
 
Andy BoettcherAndy Boettcher
So "something" is happening...(*laugh*)

What I would do is to start putting System.Debug(variable) statements all over your code to start seeing how the code is working and how it's assigning things.  Normally what ends up happening is some weird piece of data somewhere that is throwing things off.

Do some log capturing (System.Debug statements) and analysis - come back here if you have questions and we can try to help!
Becky Hammond 6Becky Hammond 6
Hi Andy!  Thank you so much for your response.  Something is definitely happening :)  I ran a debug through the sfdc debug log (not apex console).  Please bear with me - my experience with debug logs and apex is very limited.  I can see in the log that some other WF and validations are being triggered.  However I don't see any errors.  What should I look for?  
Andy BoettcherAndy Boettcher
Looking back at your original post - if you run this thing twice, do all opportunities get assigned eventually?
Becky Hammond 6Becky Hammond 6
Hi Andy!  No.  It is either 400+ assigned and the rest unassigned or 165 assigned and the rest unassigned.  I've never gotten them to all assign at once.  Seems like really strange behavior.
Becky Hammond 6Becky Hammond 6
Hi Andy - I just thought I'd follow up.  I worked with a local developer to troubleshoot the code.  He was able to determine the spot causing the issue (though he isn't yet sure why).

     if((tp != null) ) 
              //&& (tp.territory2Id != opp.Territory2Id))
              //&& (tp.moreTerritoriesAtPriority == false)
              {
                   OppIdTerritoryIdResult.put(opp.Id, tp.territory2Id);}
                   else{
                   OppIdTerritoryIdResult.put(opp.Id, null);
               }
            }
        }
        return   OppIdTerritoryIdResult;
    }

In that piece above, we commented out the criteria that checks the territory priority.  All of our accounts have only one territory but for some reason those two lines were acting like a toggle to assign and unassign the same opportunities.  Any thoughts?
EJ AufderheideEJ Aufderheide
That fixed my issue (i only have one territory per account as well).  It would be great if someone would review this apex code and fix it for those that have multiple territories per account.  Considering Salesforce sells this as part of the enterprise territory management it would be nice if it works so other don't get the run around like we have.

Thanks for the help Becky.  I look forward to an update to the code.​
JJE_OLDJJE_OLD
Hi,

Seems to me that the second condition tp.territory2Id != opp.Territory2Id is the toggle.
If your Opp is on Territory A and the Account higgest ranked Territory is A this will be false and you'll go to the else part which remove the territory from the Opportunity (set to null)
I will remove this condition.

The last condition tp.moreTerritoriesAtPriority == false means that if there is only one territory associated with your Account (which is higgest ranked) then you'll assign the territory to the Opportunity
If your Account has 2 Territories A and B with Rank 1 and no higger ranked territory, moreTerritoriesAtPriority will be true and you'll also remove the territory from the Opportunity
ssssssssssssss
+1 to Cloudy's comment. I was commenting exactly the same thing without actually reading your comment :)
But we should only comment out the tp.territory2Id != opp.Territory2Id thing as that is what removes the territory from the opps which already have been assigned territories correctly. But since it goes into the else part, it removes the territories assigned. Commenting this line resolves it. 
All other code looks correct and should handle for both scenarios, a) for accounts having one territory b) for accounts having multiple territories

Please mark this answer as solution if it helps you.
Randall MagliozziRandall Magliozzi
Thanks for everyone's contribution to this.  We spent almost 3 weeks on this including Salesforce Developer support to no avail.  Thanks all for the fix.