+ Start a Discussion
loneboatloneboat 

Formula field not updating in APEX?

The whole code is below, but I'm specifically talking about the begining of the method 'case_1()'. 

 

Just for some background:  DCS_Commission__c.CRM_Opportunity__c is a LOOKUP field to Opportunity.  DCS_Commission__c.CRM_Contract_Truck_Count__c is a number-type FORMULA field, which contains the following for the formula:  [CRM_Opportunity__c.CRM_Contract_Truck_Count__c].

 

In the beginning of case_1(), I'm changing the value on the Opportunity to which the formula refers.  But when I debug commission.CRM_Contract_Truck_Count, it is not updated in the formula.  In fact, all three debug values on lines 84-86 output 'null'.

 

How come?

 

Let me know if I need to provide additional information.

 

 

@isTest
private class CRM_DCS_Commission_Extension_Test {
    //  IDs
    private static ID userId, accountId, opportunityId, commissionId;
    
    //  sObjects
    private static User                 user;
    private static Account              account;
    private static Opportunity          opportunity;
    private static DCS_Commission__c    commission;
    private static List<DCS_Commission_SoldBy_Junction__c> 
                                        soldBys;
    
    static testMethod void testCalculateCommission() {
        Profile p = [SELECT Id from Profile WHERE Name='CRM SysAdmin'];
        
        //  create test user (can reuse the same one on several soldBys)
        user   = [SELECT Id, Name FROM User u LIMIT 1];  //  Which user doesn't really matter
        userId = user.Id;
        
        //  create test account
    	account   = new Account(
    	    Name                       = 'TestAccount'
    	   ,CRM_DCS_Relationship__c    = 'Customer'
    	);
    	//accountId = account.Id;
    	insert account;
    	
        //  create test opportunity
        opportunity   = new Opportunity(
	        accountId = accountId
	        ,Name = 'TestOpportunity'
	        ,CRM_DCS_Op_City__c = 'Happyville'
	        ,CRM_Op_State__c = 'AR'
	        ,StageName = 'Stage 1'
	        ,CloseDate = Date.valueOf('2012-01-01')
	        ,CRM_Contract_Terms__c = '5 Years'
        );
        insert opportunity;
	    opportunityId = opportunity.Id;
        
        //  create test commission
        commission   = new DCS_Commission__c();
        commission.CRM_Opportunity__c = opportunityId;
        insert commission;
        commissionId = commission.Id;
        
        case_1();
        //  configure test opportunity (fields which Commission looks at)
        
        //  configure test commission  (fields which SoldBy looks at)
        //  configure test soldBys
        //  calculateCommissions()
        //  System.assert() utility functions
        //  System.assert() commission results (incl. multipliers, payments1-3, calculated commission)
        //  (repeat for various configurations)
    }
    
    static void case_1() {
        resetSoldBys(3);
        opportunity.CRM_Contract_Truck_Count__c = 15.00;
        opportunity.CRM_Contract_Revenue__c = 15000000;
        opportunity.CRM_Contract_Margin__c  = 12.30;
        opportunity.CRM_ROIC__c             = 15.70;
        opportunity.CRM_Contract_Terms__c   = '10 years';
        
        soldBys[0].CRM_Type__c              = 'BDE';
        soldBys[0].CRM_TTM_Sold_Revenue__c  = 27000000;
        
        soldBys[1].CRM_Type__c              = 'BDE';
        soldBys[1].CRM_TTM_Sold_Revenue__c  = 1000000;
        
        soldBys[2].CRM_Type__c              = 'LEAD';
        
        //commission.Name = 'asdf';
        
        update opportunity;
        update commission;  //  TODO: do we need this, or should the formula fields automatically update??
        update soldBys;
        
        system.debug(opportunity.Id);
        system.debug(commission.CRM_Opportunity__c);
        
        system.debug(opportunity.CRM_Contract_Truck_Count__c);
        system.debug(commission.CRM_Truck_Count__c);
        system.debug(soldBys[0].CRM_Gross_Commission__c);
        
        ApexPages.StandardController sc  = new ApexPages.StandardController(commission);
        CRM_DCS_Commission_Extension ext = new CRM_DCS_Commission_Extension(sc);
        
        ext.calculateCommission_action();
        
        system.Assert(soldBys[0].CRM_Gross_Commission__c            ==  56250.00    );
        //system.Assert(soldBys[0].CRM_Multiplier__c                  ==      0.0038  );
        //system.Assert(soldBys[0].CRM_Applied_Margin_Multiplier__c   ==      1.00    );
        //system.Assert(soldBys[0].CRM_Applied_Margin_Commission__c   ==  28125.00    );
        //system.Assert(soldBys[0].CRM_ROIC_Multiplier__c             ==      1.30    );
        //system.Assert(soldBys[0].CRM_ROIC_Commission__c             ==  36562.50    );
        //system.Assert(soldBys[0].CRM_Contract_Term_Multiplier__c    ==      1.35    );
        //system.Assert(soldBys[0].CRM_Payment_1_Amount__c            == 130742.19    );
        //system.Assert(soldBys[0].CRM_Top_Performer_Multiplier__c    ==      1.50    );
        //system.Assert(soldBys[0].CRM_Calculated_Commission__c       == 130742.19    );
    }
    
    static void resetSoldBys(Integer numSoldBys) {
        //  create test soldBys
        soldBys = new List<DCS_Commission_SoldBy_Junction__c>();
        for (Integer i=0; i<numSoldBys; i++) {
        	DCS_Commission_SoldBy_Junction__c s = new DCS_Commission_SoldBy_Junction__c(
               CRM_DCS_Commission__c = commission.Id
            );
            soldBys.add(s);
        }
        
        insert soldBys;
    }
}

 

Best Answer chosen by Admin (Salesforce Developers) 
loneboatloneboat

Well I feel dumb.  Turns out I'm simply not requerying the [commission] variable AFTER the update.  Adding a SOQL query before the debug statements fixed the issue.

 

Thanks for reading!