0

Just a simple question for before update trigger:

// mapFinancialIDtoAccount and mapBillingAccounts being populated here.

for(CustomObject obj : trigger.new)
{
   Account billingAcct = mapFinancialIDtoAccount.get(obj.financialAccountID);

   if (billingAcct != null)
        obj.BillingAccountID = string.valueof(billingAcct.Id);

   if(obj.BillingAccountID != null) 
   {
        Account acct = mapBillingAccounts.get(record.BillingAccountID);
        obj.Name = acct.Name;
   }
}
update trigger.new;

Let's say billingAcct is not null but the update DML is after the for loop, will obj.BillingAccountID have value on if(obj.BillingAccountID != null) even though I've just populated it above? Or do I have to update it twice (one on population of obj.BillingAccountID and the other one to update the whole record in trigger.new)?

2
  • Which trigger event are you using, is it Before or After? In Before trigger you do not need to provide additional update statement on trigger.new. In After Trigger the records are Read Only so DML on trigger.new will throw error.
    – Sneh
    Apr 27, 2022 at 5:18
  • @Sneh I am using before trigger. So does it mean it will have value even though the update dml is in the last part of code?
    – Noobie Dev
    Apr 27, 2022 at 12:30

1 Answer 1

1

Yes you will have the value. You do not need to have update statement for trigger.new in before triggers. The value will automatically update in the records.

You can validate the values using System.debug

obj.BillingAccountID = string.valueof(billingAcct.Id);
System.debug('BillingAccountID ' + obj.BillingAccountID);

You must log in to answer this question.

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