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 isn't 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 updatable expression for the new LineEditedCount with the expression:

false

This will cause the user interface to always see that the field isn't updatable, and the value will only be updatable from script. Note that configuring the conditionally updatable expression to always return false is semantically different from unchecking the Updatable checkbox. Doing the latter, your field would never be updatable (neither from the user interface nor from script). Using the conditionally updatable expression, the updatability 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
}