0

My requirement is to insert the formula field value(Formula calculated by few other factors), in a different field on the same object. The below code is running in before update but i see many suggesting the update should be after as ,it is a formula field.

public static void fieldUpdate(List<Requested_Item__c> triggerNew, map<id, Requested_Item__c> oldMap){
   for(Requested_Item__c newchange : triggerNew) {
       if (oldMap.get(newchange.id).price_formula__c != newchange.price_formula__c && oldMap.get(newchange.id).vendor_unit_price__c != newchange.price_formula__c) {
          newchange.vendor_unit_price__c = newchange.price_formula__c;
        }
    }
1

I think you are saying you wish to copy a formula field's value to a custom field after the formula's underlying fields have been updated.

Note that you cannot update fields in the trigger object in the after update phase but the formula won't be re-evaluated until the after phase.

The simplest solution is therefore to move this copying out into a Process Builder, which will be invoked after the before and after update handling in the trigger. See step 14 in the order of execution documentation.

You will want to ensure your trigger is written to do nothing when just the vendor unit price field changes. If possible you should also restrict the process builder so it only updates the vendor unit price field when required.

1
  • Thanks Phil! thats very useful information. However, How can I move to a process builder. Process builder /Workflow or a trigger an not be triggered on formula field change. – M D Sep 22 '20 at 2:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.