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
Amit Jadhav 13Amit Jadhav 13 

How to cover formula field inside formula field in test class

for example 
Price__c * Qyt__c = Total_Price__c (Total price is formula field)
Total_Price__c * Disscount_per__c = Disscount__c (Disscount Is also formula field)
Disscount__c + Total_Price__c = Amount (Amount is  also formula field)


how to cover this filed in test class
Best Answer chosen by Amit Jadhav 13
Ajay K DubediAjay K Dubedi
Hi Amit,
When we are working with formula fields, In test class there is no need to insert a record with that particular field and also you cannot insert that record with the formula field.
You need to just insert the record with fields that are used in the formula field.
As in your case, 
Price__c * Qyt__c = Total_Price__c (Total price is formula field)
We insert a record with Price__c and Qyt__c and as you said,
Total_Price__c * Disscount_per__c = Disscount__c
This is also a formula field so you have to insert that record with Disscount_per__c field also.
Thanks,
Ajay Dubedi
 

All Answers

Tommaso BolisTommaso Bolis
Test class are used to cover apex code, not point and click configurations.
BTW you can try to retrieve a record and then add an assertion for each field checking for the expected result.
Ajay K DubediAjay K Dubedi
Hi Amit,
You have to just create Price__c and Qyt__c for creating Total_Price__c.
So to cover Total_Price__c in test class insert record with Price__c and Qyt__c field, so Total_Price__c will automatically create.
And same as others.
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks,
Ajay Dubedi
Amit Jadhav 13Amit Jadhav 13
Hey Ajay Problem happend here becuase Total_Price__c are formula and i use formula field for multipication
Total_Price__c * Disscount_per__c = Disscount__c (Disscount Is also formula field)
Ajay K DubediAjay K Dubedi
Hi Amit,
When we are working with formula fields, In test class there is no need to insert a record with that particular field and also you cannot insert that record with the formula field.
You need to just insert the record with fields that are used in the formula field.
As in your case, 
Price__c * Qyt__c = Total_Price__c (Total price is formula field)
We insert a record with Price__c and Qyt__c and as you said,
Total_Price__c * Disscount_per__c = Disscount__c
This is also a formula field so you have to insert that record with Disscount_per__c field also.
Thanks,
Ajay Dubedi
 
This was selected as the best answer