This trigger references blank fields, and complex formula fields. The reason I'm trying to do a trigger and not a formula is that it resolves to too many characters because of referenced formula fields that reference formula fields...

Some of the fields used here reference fields on the parent object (Opportunity), do I need to SELECT all the fields used by all the formulas in order for it work?

This is the first trigger I've ever tried to write, so if I'm missing something basic feel free to point that out. :)

Trigger kaPaymentTermsFormulaTLTFinalPayment on PaymentTerms__c (before update) {
    for(PaymentTerms__c record : [SELECT HowtotreatPPI__c, FinalPaymentModification__c, PPIAmount__c, TLTPayment__c 
                                  FROM PaymentTerms__c 
                                  WHERE Id IN :Trigger.newMap.keyset()
        if (record.HowtotreatPPI__c =='Pay up front out of pocket'){
              Trigger.newMap.get(record.Id).TLTFinalPaymentTest__c = record.FinalPaymentModification__c + record.PPIAmount__c + record.TLTPayment__c; 
        } else {
              record.TLTFinalPaymentTest__c = record.PPIAmount__c + record.TLTPayment__c;


And this is the error I'm getting:

Error: Invalid Data. Review all error messages below to correct your data. Apex trigger kaPaymentTermsFormulaTLTFinalPayment caused an unexpected exception, contact your administrator: kaPaymentTermsFormulaTLTFinalPayment: execution of BeforeUpdate caused by: System.NullPointerException: Argument cannot be null.: Trigger.kaPaymentTermsFormulaTLTFinalPayment: line 6, column 1

Most of the fields referenced are Currency fields, but FinalPaymentModification__c is a roll-up summary from another custom object. It didn't seem to be a problem until I tried System.assertNotEquals(Null, record.FinalPaymentModification__c); and then in the developer console it says Variable does not exist for that field.

It actually says that for TLTPayment and PPIAmount too, basically all fields but the picklist are currently blank. If I was in Formula editor I would check "treat blank fields as zeroes", where can I do that in a trigger?


I tried the trigger with fields that I knew to have values in them, and it worked, as long as I added () around the fields after 'Trigger.newMap.get(record.Id).TLTFinalPaymentTest__c = '

But adding () around the fields I need to use doesn't work, because they are blank.

I also tried creating a Workflow Rule to do the same thing, and THAT threw an error of > Division by zero Turns out that a formula referenced by a formula referenced by TLTPayment__c includes a division by a field that is blank.

  • what this doing kaPaymentTermsFormulaTLTFinalPayment?
    – Suresh
    May 22, 2015 at 16:00
  • I'm trying to update a field on the custom object PaymentTerms called TLTFinalPayment, where a normal formula would work except that it compiles to too many characters because it references other formula fields. May 22, 2015 at 16:07

2 Answers 2


The problem was division by zero. Another field that was referenced a few formulas deep was blank which caused a formula to fail due to division by zero.

One problem is that the trigger runs before update, and so the value on this field would have to be set by the user upon creation of the object. I'm going to try to run some if else statements to make sure the trigger is only run if the field in question is not blank.


Try using System.assertNotEquals(null, ...) before the for loop. Check if that helps!

  • So would I use that for each of the fields I'm selecting? i.e. System.assertNotEquals(null, CustomField1__c); System.assertNotEquals(null, CustomField2__c); System.assertNotEquals(null, CustomField3__c); etc.? May 22, 2015 at 16:05
  • So I did that, using System.assertNotEquals(Null, record.HowtotreatPPI__c); System.assertNotEquals(Null, record.FinalPaymentModification__c); System.assertNotEquals(Null, record.PPIAmount__c); System.assertNotEquals(Null, record.TLTPayment__c); and in developer console it now says variable does not exist: FinalPaymentModification__c which is weird because it didn't say that before when I was SELECTing that field. That field is a roll-up summary from another custom object. May 22, 2015 at 16:12
  • I'm aware that this answer is approaching 9 years old at this point, but for people looking at this... don't take this approach. Using an assertion in the actual trigger code wouldn't resolve the issue, it'd just give you a different exception. Use String.isBlank() or String.isNotBlank() before calling methods on strings, and use if/else or ternaries to handle nulls in numeric data.
    – Derek F
    Jan 5 at 19:49

You must log in to answer this question.

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