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
FlorentFlorent 

Update formula field in unit test

Hi all,

 

I have a custom formula field in the Opportunity object, which takes value from a field in the Account objet. This value is then checked in a validation rule.

 

But in my unit test, this validation rule always replies FALSE because the formula field value is "null"…

 

How can I update this formula field within my test class so that the trigger I'm trying to test actually runs ?

 

See here for the complete version of my question (watch out, it's very long, and you're most likely to be scared away! ,-)

Best Answer chosen by Admin (Salesforce Developers) 
FlorentFlorent

The answer to my problem can be found here, in case someone encounters a similar issue:

 

http://boards.developerforce.com/t5/Apex-Code-Development/How-to-update-a-required-formula-field-to-null-before-very-first/m-p/454473/highlight/false#M82841

 

In short, I need to assign account ID t the opportunity opp01 like this: opp01.AccountId = acc01.id, and not like I did: opp01.Account = acc01, which I assumed would allow me to access all of account acc01 values -- when in fact, no it doesn't.

All Answers

Force.comForce.com

Hi Florent,

 

Formula field gets set after the record is saved in database. However, the Validation rule gets called before the record is saved. This is the standard flow of execution in salesforce. Your validation will always FAIL since you are reading value form a formula field.

 

Possible Workaround:

You can modify your validation rule to read the field on parent account object rather than reading a formula field.

So the criteria of validation rule will be something like :

 

Account__r.<field name>

 

Thanks,

Pragati 

FlorentFlorent

Hi Pragati,

 

Thanks a lot for your answer. I did read the Triggers and Order of Execution, but hadn't understood "workflow field updates" (point 10.) are the same as (i suppose) as formula fields…

 

So I tried your workaround, but it doesn't work.

 

Log file says (user debug are done before insert). :

 

15:20:54.294 (1294517000)|USER_DEBUG|[37]|DEBUG|##### opp01.Account = Account:{Name=dsi, Account_type__c=CRO, MC_Account_code__c=AA, Id=001f0000002jzR4AAI}
15:20:54.294 (1294628000)|USER_DEBUG|[39]|DEBUG|##### opp01.Account.MC_Account_code__c = AA

 But then, validation rule fails :

 

15:20:54.369 (1369873000)|VALIDATION_RULE|03df0000000013E|MC_Check_Account_code
15:20:54.370 (1370102000)|VALIDATION_FORMULA|IF( ISPICKVAL( Group__c, "XXX Consultancy"),
    IF( ISBLANK( Account.MC_Account_code__c ),
        TRUE,
        FALSE),
    FALSE
)|Group__c=XXX Consultancy , Account.MC_Account_code__c=null
15:20:54.370 (1370122000)|VALIDATION_FAIL

 Which I really don't understand…

FlorentFlorent

The answer to my problem can be found here, in case someone encounters a similar issue:

 

http://boards.developerforce.com/t5/Apex-Code-Development/How-to-update-a-required-formula-field-to-null-before-very-first/m-p/454473/highlight/false#M82841

 

In short, I need to assign account ID t the opportunity opp01 like this: opp01.AccountId = acc01.id, and not like I did: opp01.Account = acc01, which I assumed would allow me to access all of account acc01 values -- when in fact, no it doesn't.

This was selected as the best answer