Skip to content
Groovy 5 – Get Values From Fields

Field Value Assignment Methods

Values from fields are pulled into variables or even other fields by 2 possible methods:

1) Direct Assignment

The simplest and most direct way of getting the value of a field is by using the assignment operator to put the value from the field into a variable by utilizing the field API name.

On opportunities, the name of the opportunity has its API name as Name. We just do:

def var_name = Name

If we have a custom field, the API name of a custom field is FieldName_c (custom fields always have a _c)

def var_name = FieldName_c

The variable will take the data type of the field we assign to it. So if the field is a numeric field, the variable will also be a numeric variable.

2) Using a Get Method

Groovy in Sales Cloud provides several get methods to pull values from fields:

  • getAttribute()
  • getSelectedListDisplayValue()
  • getSelectedListDisplayValues()
  • getOriginalAttributeValue()

These methods belong to the Row class, which is part of the ADF framework on which Fusion is built.

Note: These get methods always return a String. This is important because, as we will learn in later tutorials, when assigning values to fields or variables, we must respect their data types.

From all the methods listed above, the most used are getAttribute() and getSelectedListDisplayValue().

We use getAttribute() to pull values from all fields. For example, if we have a text field like the Name field of opportunities and we want to get its value, we would do:

getAttribute('Name')

If we want to assign this value to a variable, we would first define a variable and then use the assignment operator to put the value of the Name field into it:

def var_name = getAttribute('Name')

Let's see another example:

If we are in, let's say, a trigger on the opportunity object and we want to get the value of a field called Comments, we type the following code:

def var_name = getAttribute('Comments')

When we want to save our work, we get this warning: Some fields whose value may be null are not protected by the nvl() function: Comments.