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
Amol ChAmol Ch 

formula field is not getting executed in test class

formula field is not getting executed in test class, due to this condition is fails in apex class. the test class is below
@isTest(seealldata=false)
public class CreateOpty_Phyothersvcs_WPatsBatchTest {
static testMethod void test()
{
patient__C pat=new patient__C();
pat.name=test;
cost__C=100;
insert pat;

job__c jb = new job__c();
jb.name__c=test;
patient__C.id=pat.id;
//some field
insert jb;
//formula field in job object is patient_cost__c=patinet__r.cost__C

system.debug(''+jb.patient_cost__c);  //it showing null
//when i write query like

List<job__c> job = new List<job__c>();
job=[select id patient_cost__c from patient_cost__c where id=:jb.id];
 system.debug(''+job[0].patient_cost__c);  //it showing 100 correct as required

// but i want here 100
system.debug(''+jb.patient_cost__c);  //its still showing null
i've some formula field and another field update based on that formula field and thus
If formula field is Null then other field update operation will also be failed .
another it will not be cover test code coverage in apex class
in apex class i've used condtion like ex.
public class MySampleApexClass {
//some code
 List<job__c> jb=new List<job__c>();
 job=[select id,patient_cost__c from job__C where //some condition];
 for(job__c job : jb )
 {
		if(job.patient_cost__c<=50000) //this will fails(not cover in test class) due to formula field
			{ 
			
			//some code with different operation
			}
 }
 }



 
Amit GhadageAmit Ghadage
Hi Amol Ch,
In your test class  check line number 12.I am confused how your test class got saved.
Replace
​patient__C.id=pat.id;
with
​jb.patient__C.id=pat.id;

Best Regards,
Amit Ghadage.

 
George AdamsGeorge Adams
Is this your copy/pasted code? This isn't valid syntax (as far as I know):
patient__C pat=new patient__C();
pat.name=test;
cost__C=100; //how did it not throw an error here?
insert pat;

 
Amol ChAmol Ch
Hi George and Amit,

Yes this class is just an example here due to this it has error. My original class is too big due to this i put here example class. My original class  is correct not such small mistake.
i have problem in formula field not executed and due to this other field will not update.