I have a controller class that basically has some functionality which runs according to the values of two formula fields. Now when I am writing test class for it, the values of these two formula field aren't getting calculated and always return 0. I have added values for all the fields which are required to calculate the formulas and queried them after the record is inserted in Test class but in debug logs I see the values are always 0.

The formula fields:

  • Tot_SalesPrice_Calc__c
  • Tot_MarginPercent_Calc__c
  • Tot_Cost_Calc__c


Total_MainOffer_SalesPriceRounded__c + MO_Travel_Expense_Sales_Price_SCT__c


Tot_SalesPrice_Calc__c <> 0,
(Tot_SalesPrice_Calc__c - Tot_Cost_Calc__c )/Tot_SalesPrice_Calc__c,

Tot_Cost_Calc__c (used in formula above)

IF (MO_Tot_Cost__c > 0,
( MO_Tot_Cost__c - Project_Promotion__c + MO_Travel_Expense_Cost_SCT__c),

Here is some part of my Test Class:

public class SubmitforApprovalController_TestClass {
    public static Custom1__c objMO;
public static Custom1__c CreateMainOffer()
        objMO = new Custom1__c();
        objMO.Name = 'Test Offer';
        objMO.Total_MainOffer_SalesPriceRounded__c = 150000;
        objMO.MO_Tot_Cost__c = 140000;
        objMO.Project_Promotion__c = 0;
        objMO.MO_Travel_Expense_Cost_SCT__c = 0;
        //bjMO.Generic_DocGen__r.Reference_Offer_Type__c = 'Project Fire Sol';
        return objMO;

@isTest static void method1(){
          objMO = CreateMainOffer();
            objMO.Generic_DocGen__c = objGenericDocGen.id;
            insert objMO;
            Custom1__c QueriedMO = [SELECT Name,Tot_Cost_Calc__c, Tot_MarginPercent_Calc__c,Tot_SalesPrice_Calc__c FROM Custom1__c WHERE Id = :objMO.Id limit 1];
            system.debug('Marging ++++'+QueriedMO.Tot_MarginPercent_Calc__c);
            system.debug('salesprice ++++'+QueriedMO.Tot_SalesPrice_Calc__c);
  • 1
    What are you expecting the formula values to be? I calculated 10,000 / 150,000 which would be 0.07 and could be 0 depending on what the field type / decimal places are for the formula field. May 1, 2020 at 13:48

1 Answer 1


This question may help you: Refreshing Formula Fields Dynamically (without save)

You can use someSObject.recalculateFormulas(); to refresh formula values in APEX.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .