I have an object which is sometimes created by a user manually, or under some circumstances is created by a trigger. It has a formula field which gets populated by a related object's field when a user manually creates it. Because of that I am getting an error when attempting to populate it with a trigger (I get an error when trying to save the code saying the field is unwritable). Is there a way to do this without making the field a non-formula field ?

Thank you very much for your help.

  • 1
    Could you post your trigger and the error. And the formula field and what the formula field is trying to accomplish? – Chris Duncombe Oct 23 '13 at 14:56
  • 5
    Formula fields are read-only; you should be populating the fields they rely on, instead. – Mike Chale Oct 23 '13 at 14:58
  • Exactly what I was trying to get at. Wondering if you need a formula field at all or if you can just populate a field in the trigger. This depends on what you are tying to accomplish – Chris Duncombe Oct 23 '13 at 15:00
  • Yes, I need the formula field. The object is essentially used in two different ways : Sometimes it's manually created and sometimes created by a trigger. When it is created by a user it is populated by with a Opportunity__r.Stage type of formula. But then it is also created at times by a trigger on a different object which then tries to automatically populate that field with a value from a field within that object which kicks off the trigger.So still want the field to use the formula when it's created manually, but we want to populate it when it's created by the trigger.Does that make sense ? – Zoom_v Oct 23 '13 at 16:31

Formula fields are read-only; you should be populating the fields they rely on, instead.

Example field definition for a commission field (commission__c):

Value__c * commission_rate__c

Instead of writing to commission__c you would populate the values of value__c and commission_rate__c.

  • Its worth noting that the formula field wont have a value until you update the record you've filled these values out in, and requery for the formula field. – Phil Rymek Oct 23 '13 at 16:53
  • ok - this all turned out to be a mute point. As it turned out we changed the design just now and will only need the Formula value to be used. In other words, even if the record is created by a trigger it will still use the value which is provided by the field formula - not a different value supplied by the trigger (which was the original design). Therefore, I don't have to set it with code because it will just be calculated through the formula. Thanks for all of the input and feedback everybody ! – Zoom_v Oct 23 '13 at 17:05

Look at altering the formula to account for the two different scenarios. Perhaps have the trigger not populate the formula field directly, but instead populate a different field that isn't available when the record is created manually. Then use have your formula field be something like:


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.