0

For example, I am working on a method in the Account Trigger that will fire when an account record is attempted to be inserted. Ideally, I would be able to look at a field on the account record being inserted, scrub out all characters that are not digits, and then allow the record to insert with the newly edited field.

I am confident that the actual logic involving scrubbing the field is correct, but whenever I attempt to save the with the trigger active, I get an error stating- 'System.FinalException: Record is read-only'

Upon some furthering digging, it seems like this message is indicating I cannot edit the fields of an object that is firing the trigger. This leads me to the question, is there a way to edit a field on the record being inserted through a trigger? If so, how would this be done? If not, what alternative solutions would be recommended to scrub a field value of unneeded characters on insertion?

So far, I have attempted to use the isBefore trigger, thinking that since it was not yet inserted I would have a little more freedom on the fields. Once that did not allow my update, I attempted to use the isAfter by making a copy of the record, and attempting to update it once I had given the copy record the proper field values. Both of these failed with the read only error, so this is clearly not the proper technique. Happy to provide code samples if it will help, but Im guessing that this is a question that has a fairly straightforward answer I am just unable to find.

2
  • I've provided a general answer to the question. If you're looking for something more specific, then editing your question to provide the code that you've developed so far will help.
    – Derek F
    Commented Oct 25, 2018 at 20:47
  • Much appreciate the answer. I have since realized that I actually went astray before I really got to the scrubbing method, and had issues giving the trigger the correct parameters in the first place. That said, your answer was very concise and helpful to confirm that my design of the scrubbing method is on the nose. Commented Oct 25, 2018 at 20:48

2 Answers 2

2

Making adjustments to the record(s) being triggered is exactly what "Before" triggers (before insert, before update, technically before delete) are for.

Additionally, this is what the Trigger.new and Trigger.newMap trigger context variables are for. If you want to make a change to a record being triggered, you'll need to alter the record stored in one of the "new" trigger context variables. You can't add or remove records from trigger context variables.

For an insert, Trigger.newMap is not available in the before insert event.

A basic example

Trigger AccountTrigger on Account(before insert){
    for(Account acct :trigger.new){
        acct.Name = acct.Name + '- modified';
    }
}
0

Turns out, since I was building on a Trigger that was already created, I did not think about the parameters to the AccountTrigger. It was setup to only allow after update, rather than before insert, so it was breaking due to that. Once I fixed this parameter, the issue was resolved. Derek's answer would be a great answer if I asked the right question though!

You must log in to answer this question.

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