1

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

}

in apex class i've used condtion like ex.

   if(job.patient_cost__c<=50000)
   {

   }

it will not execute due to formula field

1 Answer 1

2

The formula field is evaluated when you access it. When you insert or update a record the formula field does not get evaluated, you need to query the field to get the new value.

Example:

@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;
  insert jb;
  //JOb has a formula field patient_cost__c that value of the formula is patient__c.cost__c, the only field that insert returns in the object is the id 

  //I have just inserted the job but the patient cost is empty
  System.debug('patientCost:'+jb.patient_cost__c);

  //Because I need the new value I have to refresh the object from the DB
  jb = [SELECT Name,patient_cost__c FROM job__c WHERE Id = :jb.Id];


  //Now I have the formula calculated and i am going to assert that the value of the formula is correct
  System.debug('patientCost:'+jb.patient_cost__c);
  System.assertEquals(pat.cost__C, jb.patient_cost__c);

}
4
  • I've already done in my test class also as you given in answer but my problem is that on that formula field other field update is depend then, how other field will update. and please see last sentence of my test class it showing null what about this?
    – user29314
    Jun 30, 2016 at 10:09
  • ¿Do you want update other field when the formula field change?
    – Torsen
    Jun 30, 2016 at 10:27
  • yes. correct..! and in my apex class, i'm query for job record and used formula field to compare in if condition. as i'm shown in apex class example
    – user29314
    Jun 30, 2016 at 10:47
  • You have a problem, because the formula is evaluated every time that you access the record. In your example apex class, I am understanding that you need refresh the job after insert it, because the job is not getting the formula field. This is correct because the job is in the memory and when you insert it the formula field doesn't evaluate, you need read the field to evaluate the formula field.
    – Torsen
    Jun 30, 2016 at 15:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .