Referencing the Value of a Field in the Current Object

When writing scripts that execute in the context of the current business object, you can reference the value of any field in the current object by simply using its API name. This includes all of the following contexts:

  • object validation rules

  • field-level validation rules

  • formula field expressions

  • custom field conditionally updateable expressions

  • custom field conditionally required expressions

  • object triggers

  • field triggers

  • object functions, and

  • conditions for executing an object workflow

The API name of custom fields that you have added to a standard object will be suffixed with _c to distinguish them from standard field names. So, for example to write a script that references the value of a standard field named ContactPhoneNumber and a custom field named ContactTwitterName, you would use the following code:

// Assign value of standard field "ContactPhoneNumber" to "phone" var
def phone = ContactPhoneNumber

// Assign value of custom field "ContactTwitterName" to "twitterName" var
def twitterName = ContactTwitterName_c

// Assemble text fragment by concatenating static text and variables
def textFragment = 'We will try to call you at ' + phone +
                   ' or send you a tweet at ' + twitterName

Defining a local variable to hold the value of a field is a good practice if you will be referencing its value more than once in your code. If you only need to use it once, you can directly reference a field's name without defining a local variable for it, like this:

def textFragment = 'We will try to call you at ' + ContactPhoneNumber +
                   ' or send you a tweet at ' + ContactTwitterName_c
Note: When referencing a field value multiple times, you can generally choose to use or not to use a local variable according to your own preference, however when working with an ADF RowIterator object, you must to use the def keyword to define a variable to hold it. See the tip in the Using Substitution Expressions in Strings for more information.