1

I've written this trigger, but there's an error that I can't resolve

The Trigger:

trigger AmounTtoWords on Receipt__c (before insert, before update) {
  for (Receipt__c **:** Trigger.new) {
        if (Total_Amount_Due__c != null && Total_Amount_Due__c >= 0) {
         
            Long n = c.Amount.longValue();
            string amo = ConvertCurrencyToWords.english_number(n);
            string amo1 = amo.remove(',');
            c.Amount_in_Words__c = amo1;
        } else {
            c.Amount_in_Words__c = null;
        }
    }
}

The Error:

enter image description here

2
  • 3
    You need to name the variable like for (Receipt__c receipt : Trigger.new) then use that to access the various field values like receipt.Total_Amount_Due__c etc. You also reference an undeclared variable c in some of this code.
    – Phil W
    Commented Jan 20, 2023 at 8:42
  • In addition to @PhilW comment, you are referencing fields Total_Amount_Due__c without variable, so if you name your Receipt__c, for example, Receipt__c receipt, then you should reference to the field as receipt.Total_Amount_Due__c. Commented Jan 20, 2023 at 8:48

1 Answer 1

2

As others mentioned in the comments already, you need to give a name to your variable. Also, you need to specify on which record you want to reference that field. So fixed code would look like:

for (Receipt__c c: Trigger.new) {
    if (c.Total_Amount_Due__c != null && c.Total_Amount_Due__c >= 0) {
        // Rest of your logic here
    }
}

Also, I guess that c should be your receipt, so I called it like that to minimize the changes you would need to do to your code. But please give your variables some meaningful names.
And think about using some sort of a trigger framework, best practice is to have 1 line triggers that call your trigger framework and not having any logic inside your triggers.

1

You must log in to answer this question.

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