+ 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
public class CreateOpty_Phyothersvcs_WPatsBatchTest {
static testMethod void test()
patient__C pat=new patient__C();
insert pat;

job__c jb = new job__c();
//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.

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();
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.