+ Start a Discussion
HamptonHampton 

Question on Apex & Custom Formula Fields

Hello:

 

I have the following Apex issue I was looking for some guidance on.

 

The fields Copper_Voice_Penetration__c, Copper_CBAD_Penetration__c, Fiber_Voice_Penetration__c, Fiber_CBAD_Penetration__c, DSL_Penetration__c, Fiber_Internet_Penetration__c and Video_Penetration__c are all custom formula fields yet I need their values in my IF statements to do the calculations.

 

I have done some reading and now realize that those formula field values aren't calculated until after insert and I am running a before insert apex. Are there any other workarounds to this or any other suggestions anyone can provide?

 

Thanks,

 

Hampton

 

trigger RevShareUpdate on Penetration_Summary__c (before insert, before update) {

Set<String> penSumName = new Set<String>(); 

for (Penetration_Summary__c pen : Trigger.new) {
        penSumName.add(pen.Name);
}
Map<String, Video_Release__c> releaseSumMap = new Map<String, Video_Release__c>();
for(Video_Release__c release1:[Select ID, Name, Video_Qualified_Addresses__c, Copper_Voice_Rev_Share__r.Low_End_Tier_One__c, Copper_Voice_Rev_Share__r.Low_End_Tier_Two__c,
Copper_Voice_Rev_Share__r.Low_End_Tier_Three__c, Copper_Voice_Rev_Share__r.High_End_Tier_One__c,
Copper_Voice_Rev_Share__r.High_End_Tier_Two__c, Copper_Voice_Rev_Share__r.High_End_Tier_Three__c,
Copper_Voice_Rev_Share__r.Tier_One__c, Copper_Voice_Rev_Share__r.Tier_Two__c,
Copper_Voice_Rev_Share__r.Tier_Three__c, DSL_Rev_Share__r.Low_End_Tier_One__c, DSL_Rev_Share__r.Low_End_Tier_Two__c,
DSL_Rev_Share__r.Low_End_Tier_Three__c, DSL_Rev_Share__r.High_End_Tier_One__c,
DSL_Rev_Share__r.High_End_Tier_Two__c, DSL_Rev_Share__r.High_End_Tier_Three__c,
DSL_Rev_Share__r.Tier_One__c, DSL_Rev_Share__r.Tier_Two__c,
DSL_Rev_Share__r.Tier_Three__c, Fiber_Voice_Rev_Share__r.Low_End_Tier_One__c, Fiber_Voice_Rev_Share__r.Low_End_Tier_Two__c,
Fiber_Voice_Rev_Share__r.Low_End_Tier_Three__c, Fiber_Voice_Rev_Share__r.High_End_Tier_One__c,
Fiber_Voice_Rev_Share__r.High_End_Tier_Two__c, Fiber_Voice_Rev_Share__r.High_End_Tier_Three__c,
Fiber_Voice_Rev_Share__r.Tier_One__c, Fiber_Voice_Rev_Share__r.Tier_Two__c,
Fiber_Voice_Rev_Share__r.Tier_Three__c, Fiber_Internet_Rev_Share__r.Low_End_Tier_One__c, Fiber_Internet_Rev_Share__r.Low_End_Tier_Two__c,
Fiber_Internet_Rev_Share__r.Low_End_Tier_Three__c, Fiber_Internet_Rev_Share__r.High_End_Tier_One__c,
Fiber_Internet_Rev_Share__r.High_End_Tier_Two__c, Fiber_Internet_Rev_Share__r.High_End_Tier_Three__c,
Fiber_Internet_Rev_Share__r.Tier_One__c, Fiber_Internet_Rev_Share__r.Tier_Two__c,
Fiber_Internet_Rev_Share__r.Tier_Three__c,Video_Rev_Share__r.Low_End_Tier_One__c, Video_Rev_Share__r.Low_End_Tier_Two__c,
Video_Rev_Share__r.Low_End_Tier_Three__c, Video_Rev_Share__r.Low_End_Tier_Four__c,
Video_Rev_Share__r.Low_End_Tier_Five__c,Video_Rev_Share__r.High_End_Tier_One__c,
Video_Rev_Share__r.High_End_Tier_Two__c, Video_Rev_Share__r.High_End_Tier_Three__c,
Video_Rev_Share__r.High_End_Tier_Four__c, Video_Rev_Share__r.High_End_Tier_Five__c,
Video_Rev_Share__r.Tier_One__c, Video_Rev_Share__r.Tier_Two__c,
Video_Rev_Share__r.Tier_Three__c, Video_Rev_Share__r.Tier_Four__c, 
Video_Rev_Share__r.Tier_Five__c, CBW_Rev_Share__r.Tier_One__c, CBW_Rev_Share__r.Low_End_Tier_One__c, 
CBW_Rev_Share__r.High_End_Tier_One__c
from Video_Release__c Where Name IN :penSumName and Rev_Share_Paid__c = TRUE]) {
    releaseSumMap.put(release1.Name, release1);   
}

List<Penetration_Summary__c> penForUpdate = new List<Penetration_Summary__c>();

for(Penetration_Summary__c c : trigger.new)
{
 if(releaseSumMap.containsKey(c.Name))
 {
    if(releaseSumMap.get(c.Name).Copper_Voice_Rev_Share__r.Tier_One__c != null && c.Copper_Voice_Rev__c != null)
 {
   if(c.Copper_Voice_Penetration__c >=(releaseSumMap.get(c.Name).Copper_Voice_Rev_Share__r.Low_End_Tier_One__c) && c.Copper_Voice_Penetration__c <= (releaseSumMap.get(c.Name).Copper_Voice_Rev_Share__r.High_End_Tier_One__c))
        c.Copper_Voice_Rev_Share__c = ((releaseSumMap.get(c.Name).Copper_Voice_Rev_Share__r.Tier_One__c) /100) * (c.Copper_Voice_Rev__c+c.copper_CBAD_Rev__c);
    else {
    if(releaseSumMap.get(c.Name).Copper_Voice_Rev_Share__r.Tier_Two__c != null && c.Copper_Voice_Rev__c != null)
 {
    if(c.Copper_Voice_Penetration__c >= releaseSumMap.get(c.Name).Copper_Voice_Rev_Share__r.Low_End_Tier_Two__c && c.Copper_Voice_Penetration__c <= releaseSumMap.get(c.Name).Copper_Voice_Rev_Share__r.High_End_Tier_Two__c)
        c.Copper_Voice_Rev_Share__c = ((releaseSumMap.get(c.Name).Copper_Voice_Rev_Share__r.Tier_Two__c) /100) * (c.Copper_Voice_Rev__c+c.copper_CBAD_Rev__c);
    else {
   if(releaseSumMap.get(c.Name).Copper_Voice_Rev_Share__r.Tier_Three__c != null && c.Copper_Voice_Rev__c != null)
 {
        c.Copper_Voice_Rev_Share__c = ((releaseSumMap.get(c.Name).Copper_Voice_Rev_Share__r.Tier_Three__c) /100) * (c.Copper_Voice_Rev__c+c.copper_CBAD_Rev__c); }
         }
        }
       }
 {
   if(releaseSumMap.get(c.Name).DSL_Rev_Share__r.Tier_One__c != null && c.DSL_Rev__c != null)        
 {
   if(c.DSL_Penetration__c >=(releaseSumMap.get(c.Name).DSL_Rev_Share__r.Low_End_Tier_One__c) && c.DSL_Penetration__c <= (releaseSumMap.get(c.Name).DSL_Rev_Share__r.High_End_Tier_One__c))
        c.DSL_Rev_Share__c = ((releaseSumMap.get(c.Name).DSL_Rev_Share__r.Tier_One__c) /100) * c.DSL_Rev__c;
    else {
    if(releaseSumMap.get(c.Name).DSL_Rev_Share__r.Tier_Two__c != null && c.DSL_Rev__c != null)        
 {
    if(c.DSL_Penetration__c >= releaseSumMap.get(c.Name).DSL_Rev_Share__r.Low_End_Tier_Two__c && c.DSL_Penetration__c <= releaseSumMap.get(c.Name).DSL_Rev_Share__r.High_End_Tier_Two__c)
        c.DSL_Rev_Share__c = ((releaseSumMap.get(c.Name).DSL_Rev_Share__r.Tier_Two__c) /100) * c.DSL_Rev__c;
    else {    
    if(releaseSumMap.get(c.Name).DSL_Rev_Share__r.Tier_Three__c != null && c.DSL_Rev__c != null)      
 {  
        c.DSL_Rev_Share__c = ((releaseSumMap.get(c.Name).DSL_Rev_Share__r.Tier_Three__c) /100) * c.DSL_Rev__c; }
         }
        }
       }    
      }
     }
    }
  { 
    if(releaseSumMap.get(c.Name).Fiber_Voice_Rev_Share__r.Tier_One__c != null && c.Fiber_Voice_Rev__c != null)
 {
    if(c.Fiber_Voice_Penetration__c >=(releaseSumMap.get(c.Name).Fiber_Voice_Rev_Share__r.Low_End_Tier_One__c) && c.Fiber_Voice_Penetration__c <= (releaseSumMap.get(c.Name).Fiber_Voice_Rev_Share__r.High_End_Tier_One__c))
        c.Fiber_Voice_Rev_Share__c = ((releaseSumMap.get(c.Name).Fiber_Voice_Rev_Share__r.Tier_One__c) /100) * (c.Fiber_Voice_Rev__c+c.Fiber_CBAD_Rev__c);
    else {
   if(releaseSumMap.get(c.Name).Fiber_Voice_Rev_Share__r.Tier_Two__c != null && c.Fiber_Voice_Rev__c != null)
 {
    if(c.Fiber_Voice_Penetration__c >= releaseSumMap.get(c.Name).Fiber_Voice_Rev_Share__r.Low_End_Tier_Two__c && c.Fiber_Voice_Penetration__c <= releaseSumMap.get(c.Name).Fiber_Voice_Rev_Share__r.High_End_Tier_Two__c)
        c.Fiber_Voice_Rev_Share__c = ((releaseSumMap.get(c.Name).Fiber_Voice_Rev_Share__r.Tier_Two__c) /100) * (c.Fiber_Voice_Rev__c+c.Fiber_CBAD_Rev__c);
    else {    
    if(releaseSumMap.get(c.Name).Fiber_Voice_Rev_Share__r.Tier_Three__c != null && c.Fiber_Voice_Rev__c != null)
 {
        c.Fiber_Voice_Rev_Share__c = ((releaseSumMap.get(c.Name).Fiber_Voice_Rev_Share__r.Tier_Three__c) /100) * (c.Fiber_Voice_Rev__c+c.Fiber_CBAD_Rev__c); }
         }
        }
       }    
      }
     }
  {  
   if(releaseSumMap.get(c.Name).Fiber_Internet_Rev_Share__r.Tier_One__c != null && c.Fiber_Internet_Rev__c != null)
 {
   if(c.Fiber_Internet_Penetration__c >=(releaseSumMap.get(c.Name).Fiber_Internet_Rev_Share__r.Low_End_Tier_One__c) && c.Fiber_Internet_Penetration__c <= (releaseSumMap.get(c.Name).Fiber_Internet_Rev_Share__r.High_End_Tier_One__c))
        c.Fiber_Internet_Rev_Share__c = ((releaseSumMap.get(c.Name).Fiber_Internet_Rev_Share__r.Tier_One__c) /100) * c.Fiber_Internet_Rev__c;
    else {
   if(releaseSumMap.get(c.Name).Fiber_Internet_Rev_Share__r.Tier_Two__c != null && c.Fiber_Internet_Rev__c != null)
 {
    if(c.Fiber_Internet_Penetration__c >= releaseSumMap.get(c.Name).Fiber_Internet_Rev_Share__r.Low_End_Tier_Two__c && c.Fiber_Internet_Penetration__c <= releaseSumMap.get(c.Name).Fiber_Internet_Rev_Share__r.High_End_Tier_Two__c)
        c.Fiber_Internet_Rev_Share__c = ((releaseSumMap.get(c.Name).Fiber_Internet_Rev_Share__r.Tier_Two__c) /100) * c.Fiber_Internet_Rev__c;
    else {    
   if(releaseSumMap.get(c.Name).Fiber_Internet_Rev_Share__r.Tier_Three__c != null && c.Fiber_Internet_Rev__c != null)
 {
        c.Fiber_Internet_Rev_Share__c = ((releaseSumMap.get(c.Name).Fiber_Internet_Rev_Share__r.Tier_Three__c) /100) * c.Fiber_Internet_Rev__c; }
         }     
        }
       }
      }
     }
  {
   if(releaseSumMap.get(c.Name).Video_Rev_Share__r.Tier_One__c != null && c.Video_Rev__c != null)    
 {
  if(c.Video_Penetration__c >=(releaseSumMap.get(c.Name).Video_Rev_Share__r.Low_End_Tier_One__c) && c.Video_Penetration__c <= (releaseSumMap.get(c.Name).Video_Rev_Share__r.High_End_Tier_One__c))
        c.Video_Rev_Share__c = ((releaseSumMap.get(c.Name).Video_Rev_Share__r.Tier_One__c) /100) * c.Video_Rev__c;
    else {
   if(releaseSumMap.get(c.Name).Video_Rev_Share__r.Tier_Two__c != null && c.Video_Rev__c != null)    
 {
    if(c.Video_Penetration__c >= releaseSumMap.get(c.Name).Video_Rev_Share__r.Low_End_Tier_Two__c && c.Video_Penetration__c <= releaseSumMap.get(c.Name).Video_Rev_Share__r.High_End_Tier_Two__c)
        c.Video_Rev_Share__c = ((releaseSumMap.get(c.Name).Video_Rev_Share__r.Tier_Two__c) /100) * c.Video_Rev__c;
    else { 
   if(releaseSumMap.get(c.Name).Video_Rev_Share__r.Tier_Three__c != null && c.Video_Rev__c != null)    
 {
   if(c.Video_Penetration__c >= releaseSumMap.get(c.Name).Video_Rev_Share__r.Low_End_Tier_Three__c && c.Video_Penetration__c <= releaseSumMap.get(c.Name).Video_Rev_Share__r.High_End_Tier_Three__c)
        c.Video_Rev_Share__c = ((releaseSumMap.get(c.Name).VIDEO_Rev_Share__r.Tier_Three__c) /100) * c.Video_Rev__c;
   else { 
   if(releaseSumMap.get(c.Name).Video_Rev_Share__r.Tier_Four__c != null && c.Video_Rev__c != null)    
 {
   if(c.Video_Penetration__c >= releaseSumMap.get(c.Name).Video_Rev_Share__r.Low_End_Tier_Four__c && c.Video_Penetration__c <= releaseSumMap.get(c.Name).Video_Rev_Share__r.High_End_Tier_Four__c)
        c.Video_Rev_Share__c = ((releaseSumMap.get(c.Name).Video_Rev_Share__r.Tier_Four__c) /100) * c.Video_Rev__c;
 else {
   if(releaseSumMap.get(c.Name).Video_Rev_Share__r.Tier_Five__c != null && c.Video_Rev__c != null)    
{
        c.Video_Rev_Share__c = ((releaseSumMap.get(c.Name).Video_Rev_Share__r.Tier_Five__c) /100) * c.Video_Rev__c; }
         }
        }
       }
      } 
     }
    }
   }
  }
 }
   if(releaseSumMap.get(c.Name).CBW_Rev_Share__r.Tier_One__c != null && c.CBW_Rev__c != null)          
 { 
       c.CBW_Rev_Share__c = ((releaseSumMap.get(c.Name).CBW_Rev_Share__r.Tier_One__c) /100) * c.CBW_Rev__c;
 }
       c.Units__c = (releaseSumMap.get(c.Name).Video_Qualified_Addresses__c);
   penForUpdate.add(c);
   }
  } 
}

 

Avidev9Avidev9
well why arent you considering the after event ?
In after event you will get all the formula values but you may have to do an explicit update on the records and also consider the case of recursion.
souvik9086souvik9086

Before insert/update it is not possible to get the Formula field values. It is always advisable to use after insert/update in case of using conditions for formula fields.

 

If this post solves your problem kindly mark it as solution. if this post is helpful please throw Kudos.

Thanks