Avoiding use of future calls in triggers?

I have some code I’d like to change in a trigger. The issue is that there is a field that is being updated with a prefix plus the value of a field in the object that is an automatic number. The problem is that the auto number is not assigned until after the insert but the field needs to be set before the user sees the values in the form. We’re using future calls to handle it because you can’t do updates on records that are in the isAfter state. If we use the future calls, there are times the user sees invalid values in the form until after the future call runs and the form is then updated.

How would one avoid this problem?


When I’ve had need for this, I’ve always used the following pattern, which works out well:

trigger X on Y (after insert, before update) {
    if(Trigger.isAfter && Trigger.isInsert) {
        update Trigger.new.deepClone(true, false, false);
    if(Trigger.isBefore && Trigger.isUpdate) {
        for(Y record: Trigger.new) {
            // Change the value for record

You could also prevent recursion with a static field, but this may not be necessary if you use this design.

Source : Link , Question Author : Robert Harper , Answer Author : sfdcfox

Leave a Comment