Implementing Non-Formula Field Changeable Only From Script

Assume you want to add a LineEditedCount field to the OrderLineItem object to track how many times it has been edited. The field value needs to be stored in the database, so a formula field is not appropriate.

Start by adding a custom field of type Number to the object, configuring its default value to be the literal value 0 (zero). Next, configure a conditionally updateable expression for the new LineEditedCount with the expression:

false

This will cause the user interface to always see that the field is not updateable, and the value will only be updateable from script. Note that configuring the conditionally updateable expression to always return false is semantically different from unchecking the Updateable checkbox. Doing the latter, your field would never be updateable (neither from the user interface nor from script). Using the conditionally updateable expression, the updateability enforcement is done only at the user interface level.

Finally, to derive the value of the LineEditedCount you would add the following trigger:

  • Trigger Object: OrderLineItem

  • Trigger: Before Update In Database

  • Trigger Name: Before_Update_Adjust_Edited_Count

Trigger Definition

adf.util.logStart('Before_Update_Adjust_Edited_Count')
// Get the original value of the LineEditedCount field
def origCount = getOriginalAttributeValue('LineEditedCount_c')
def newCount = origCount + 1
// Only assign the value if it's not already what we want it to be
if (LineEditedCount_c != newCount) {
  LineEditedCount_c = newCount
}