0

I'm trying to write Unit Test which Involves doing an assertion on the formula field value.I know the formula field values will be calculated on fly.So I basically did an update on the record so that when I query for the record again I'd get the value.But even after the update when I try asserting the formula field the value being returned is NULL.

By the way I even queried the field.

Any thoughts on this.

Account acc = new Account(Name='Test Account',Company;='Test company');

Now I create a Contact for this Account .the contact has a formula field that takes the company value from Account.

Contact conRec = [Select Id,Company__c from Contact where AccountId=:acc.Id]; 

At this point I won't get any value so what I did is just an update on the contact like

conRec.Email='[email protected]'

....After this I queried for the same contact

c1 = [Select Id,Company__c from Contact where Ic=:conRec.Id]; System.assertEquals(c1.Company__c,acc.Company);

This fails because i get a null value

3
  • 1
    can you post code ? Jun 16, 2017 at 21:36
  • Account acc = new Account(Name='Test Account',Company;='Test company'); Now I create a Contact for this Account .the contact has a formula field that takes the company value from Account. Contact conRec = [Select Id,Company__c from Contact where AccountId=:acc.Id]; //At this point I won't get any value so what I did is just an update on the contact like conRec.Email='[email protected]'....After this I queried for the same contact c1 = [Select Id,Company__c from Contact where Ic=:conRec.Id]; System.assertEquals(c1.Company__c,acc.Company);//This fails because i get null value
    – sai kumar
    Jun 16, 2017 at 21:39
  • At what point do you insert/upsert conRec?
    – o-lexi
    Jun 16, 2017 at 22:32

1 Answer 1

1

Is your code similar to this?

Account acc = new Account(
    Name = 'Test Account',
    Company__c = 'company name'
);

insert acc;

Contact con = new Contact(
    LastName = 'Test', 
    Account = acc.Id
);

insert con;

con = [select Formula_Field__c from Contact where Id = :con.Id];

System.assertEquals(Acc.Company__c, con.Formula_Field__c);

You must log in to answer this question.

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