I've to insert, for my test method, an Invoice__c object with a value of Amount_Due__c more than 0. This field is a Formula (Number) with 2 Decimal Places field with this syntax Invoice_Amount__c - Collected_Amount__c.

Collected_Amount__c is a simple Number(18, 0) field
Invoice_Amount__c is Number(16, 2) field

Invoice_Amount__c is not related to anything, neither is Collected_Amount__c.

My insert statement:

for(Integer i = 0; i < bulkDataValue; i++){
    forCollectionInvoices_tst.add(new Invoice__c(Quotation__c = newQuotation_tst[i].Id, Invoice_Amount__c = 20.00, Collected_Amount__c = 1));

My debuging process came with this results:
What's happening is, I was able to insert the desired Collected_Amount__c value, but Invoice_Amount__c value still equal to 0.

I made a goofing around move with inserting a negative value to Collected_Amount__c and it works, but I need to insert right logical acceptable values.

Thanx in advance!


Formula fields are evaluated when an SObject is queried not when an SObject is modified in memory. So in the past, if you needed the result of a formula field evaluation in your business logic or in a test you had to re-query. But recently the SObject.recalculateFormulas method was added:

Recalculates all formula fields on an sObject, and sets updated field values. Rather than inserting or updating objects each time you want to test changes to your formula logic, call this method and inspect your new field values. Then make further logic changes as needed.

Calling that may solve your problem.

(Not sure this matches all your symptoms.)

| improve this answer | |
  • The formula field is responding to me, but my problem is: I'm not able to insert or change the value of Invoice_Amount__c, and I'm not sure why! – MAZux Jul 8 '16 at 13:29
  • @MAZux OK I'll delete this in a little while. If you are inserting a value of say 20 but when you query again you get zero then I assume there is a workflow rule or trigger that e.g. maps all positive values to zero. – Keith C Jul 8 '16 at 13:58
  • There isn't any validation rules or triggers related with this field or the other mentioned fields. @Keith – MAZux Jul 8 '16 at 14:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.