We've implemented up to 3 tier hierarchy opportunities, and need to roll up children and grandchildren up to the 1st tier opportunity. A lookup's in the opportunity to relate to the higher tier. We should be able to walk up the relationship using ' __r ' notation...
I wrote a code snippet to run on before insert and tag a record as [Parent (Tier 1) | Child (Tier 2)| Grandchild (Tier 3)].
Here's the problem: when a 3rd tier opportunity is fed to trigger.new on insert, the code below is skipping right to 2nd Tier. Apex is ignoring that there is a lookup value on the 2nd Tier opportunity. It is incorectly tagging grandchildren as Tier 2.
I and a few others saw nothing wrong with the logic. Even though @JeffMolis posted on how to do something similar with a formula, Would appreciate if someone throws a tip my way.
https://trailblazers.salesforce.com/answers?id=90630000000ChkPAAS
Thanks
...
if (Trigger.isBefore && Trigger.isInsert ) {
// Update new opportunities with their tier hierarchy
system.debug('******************Opportunity Before Insert*********************** ');
if(trigger.size > 0){
system.debug('******************Records Found in Trigger*********************** ');
for (Opportunity o : trigger.new) {
If (o.Primary_Opportunity__r.Primary_Opportunity__c != Null){
system.debug('******************GrandChild*********************** ');
o.TEST_Tier_Level__c = 'Tier3';
o.TopHierarchyOpportunity__c = o.Primary_Opportunity__r.Primary_Opportunity__c;
}
Else If (o.Primary_Opportunity__c != Null){
system.debug('******************Child*********************** ');
o.TEST_Tier_Level__c = 'Tier2';
o.TopHierarchyOpportunity__c = o.Primary_Opportunity__c;
}
}
// And then the Else stateent tags it as Tier 1
Hi Rafael,
I think the problem may stem from the assumption that the trigger fetches the values of the parent opportunity record. There is no uery that would fetch the grant parent opportunity.
A formula solution would be an efficient way of achieving this, though you could also make another Apex query to fetch the data you need to 3 tiers before your loop.