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.