Skip to main content
added 18 characters in body
Source Link
Kris Goncalves
  • 22.8k
  • 8
  • 35
  • 72

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
 

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
)
 

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),
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);
 }
}

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

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
)

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),
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);
 }
}

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
 

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
)
 

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),
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);
 }
}
deleted 50 characters in body
Source Link
Kris Goncalves
  • 22.8k
  • 8
  • 35
  • 72

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 twoThe formula fields with formulas:

  • Tot_SalesPrice_Calc__c
  • Tot_MarginPercent_Calc__c
  • Tot_Cost_Calc__c

Tot_SalesPrice_Calc__c = Total_MainOffer_SalesPriceRounded__c + MO_Travel_Expense_Sales_Price_SCT__cTot_SalesPrice_Calc__c

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

Field Tot_Cost_Calc__c used in formula field Tot_MarginPercent_Calc__c is also a formula field with formula: Tot_Cost_Calc__c = IFTot_MarginPercent_Calc__c

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

Tot_Cost_Calc__c (MO_Tot_Cost__c > 0,( MO_Tot_Cost__c - Project_Promotion__c + MO_Travel_Expense_Cost_SCT__c) , 0used in formula above)

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);
 }
}

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);
 }
}

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

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
)

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),
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);
 }
}
Source Link
Rahul
  • 19
  • 1
  • 8

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);
 }
}