Values from fields are pulled into variables or even other fields by 2 possible methods:
1 ) we directly assign the field to a variable or to another field using = or a set method
2 ) we use a get method
Lets explore each of this options.
1 Use direct assignment
The simplest and most direct way of getting the value of a field is by using 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 would have a Costume 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 just take the data type of the field we assign into it.
So if the field is a numeric field the variable will also be a numeric variable
2 Use a get method
The groovy in sales cloud puts at our disposal few get methods, to pull values from fields.
This methods belong to the Row class which is a class that is part of the ADF framework, on which fusion is built.
To be noted that this get methods always return a String.
This is very important, because as we will learn in my next tutorials, when we assign values into fields or variables we must respect their data types.
So, if, for example, I want to assign a text into a field that is of type number, an error will be thrown by the application, something in the lines of: java.lang.NumberFormatException
But about what methods return and how data types work together we will learn in a later lesson, for now, just remember that those methods will give us back a String
From all of the methods listed above the most used are getAttribute() and getSelectedListDisplayValue()
We use getAttribute(), to pull values from all fields.
So if we have a text field, like the Name field of opportunities and we want to get its value we would do:
If we want to assign this value to a variable we would first define a variable and then use assignment operator to put the value of Name field into it.
def var_name = getAttribute('Name')
Lest see another example:
Now if we are in, lets 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