function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Rahul Bhattad 4Rahul Bhattad 4 

Formula fields in Test class is not calculating correctly even after querying them

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.

Tot_SalesPrice_Calc__c and Tot_MarginPercent_Calc__c are the two formula fields with formulas:

Tot_SalesPrice_Calc__c = Total_MainOffer_SalesPriceRounded__c + MO_Travel_Expense_Sales_Price_SCT__c

Tot_MarginPercent_Calc__c = IF( Tot_SalesPrice_Calc__c <> 0, (Tot_SalesPrice_Calc__c - Tot_Cost_Calc__c )/Tot_SalesPrice_Calc__c, 0 )

Field Tot_Cost_Calc__c used in formula field Tot_MarginPercent_Calc__c is also a formula field with formula: Tot_Cost_Calc__c = IF (MO_Tot_Cost__c > 0,( MO_Tot_Cost__c - Project_Promotion__c + MO_Travel_Expense_Cost_SCT__c) , 0)

Here is some part of my Test Class:
@isTest
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;
        objMO.MO_Type_SCT__c='O2O';
        //bjMO.Generic_DocGen__r.Reference_Offer_Type__c = 'Project Fire Sol';
        return objMO;
      }

@isTest static void method1(){
        objUser=CreateUser();
        System.runAs(objUser){
          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);
 }
}

 
Best Answer chosen by Rahul Bhattad 4
David Zhu 🔥David Zhu 🔥
You may try inserting the record in anoymous Window and check if the formular field is zero. It could be a glitch in your formular field.

All Answers

{tushar-sharma}{tushar-sharma}
I suggest you two things here:
1. Checkin your foumla Treat blank fields as zero should be selected.
2. Populate this field as well "MO_Travel_Expense_Sales_Price_SCT__c"

If this answer helps you, please mark it as accepted.

Regards,
Tushar Sharma
https://newstechnologystuff.com/
David Zhu 🔥David Zhu 🔥
You may try inserting the record in anoymous Window and check if the formular field is zero. It could be a glitch in your formular field.
This was selected as the best answer
Rahul Bhattad 4Rahul Bhattad 4
Hi David,

That actually worked. I tried inserting record through anoymous Window and was surprised to see that Tot_SalesPrice_Calc__c was calculated as 0 and ofcouse Tot_MarginPercent_Calc__c  was 0 due to this. So I tried to dig deeper and found out that the field  Total_MainOffer_SalesPriceRounded__c  is calculated in Before Update trigger. So I added one more field in the Test record after inserting the objMO that was basically setting up the value for Total_MainOffer_SalesPriceRounded__c in trigger and updated the record and Voila that worked.

Thanks for this valueable hint and stay safe! :)

Best Regards,
Rahul