This property is a list of records of the opportunity type that contains all records that are part of the current trigger execution (and that have the most recent field values). A list is just a complex Apex type that is used to store a set of items of the same type. These elements can be taken using the [n] operator, where n is the element’s index, which spans from 0 (the first element) to the maximum number of elements minus 1 (the last element).
Basically, on line 1 we are storing the first element of the saved opportunities in a single variable of the opportunity type called currentOpp.
On line 8, the before event section starts, and on line 11 we are executing a piece of code for insert and update operations. Basically, if the current opportunity amount is less than 0, we add an error to the opportunity stating that the value is not correct. This check could be done using validation rules as well.
The addError() method (a method is another way of calling a function, as you saw with formulas and validation rules, but methods are related to a specific Apex object) is used to attach an error to the record (that is displayed on the page layout, as we’ll see shortly), flagging it as not committable to the database, thus interrupting the order of execution. We saw this in the previous section.
From line 23, we are concentrating on the before update condition (that is, if we create a record, this piece of code won’t be executed). We can see a new variable that stores the old valued opportunity (Trigger.old is a list just like Trigger.new, but it contains the opportunities with the old values) and two new double variables that contain the new and old values.
The Trigger Apex class supports many static variables that can be used to understand the executing context: the most common ones are Trigger.newMap and Trigger.oldMap, which convey the same records as Trigger.new and Trigger.old but using a special Apex class called Map, which organizes records based on their Salesforce ID field (which in some algorithms is really useful). For more information, refer to Salesforce Help at https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_class_System_Trigger.htm.
Why has the oldOpportunity variable been defined inside the Trigger.isUpdate if statement and not just after the currentOpp definition? This is because insert operations do not convey an old value (there is nothing on the database so far), thus accessing the Trigger.old list in an insert operation leads to a critical error on Apex execution, which interrupts trigger execution and prevents the whole save operation from closing.
The two newAmount and oldAmount variables are then compared, and if the new amount is less than 50% of the previous amount, an error is attached to the record (this match could have been implemented using a validation rule as well).
Let’s test it out:
- Open the Developer Console (click the Settings icon and then select the Developer Console item). When the console loads, click on File | New | Apex Trigger, select the Opportunity sObject, and call it OpportunityTrigger:
Trigger creation
2. Now copy the whole code in the previous example (or cut and paste it from the book’s GitHub repository) and click File | Save or just Ctrl + S on your keyboard. If you have made some errors while writing your code, you’ll see where the error lies on the Problems tab:
List of problems generated by miswritten code
Line 5 simply didn’t terminate with a semicolon (required by Apex syntax), and this leads to a chain of errors on the subsequent lines: if we add the semicolon, all the compile errors disappear.
Do you remember debug logs from Chapter 2, Auditing and Monitoring? When we open the Developer Console, a new debug log is opened on the fly (have a look at the Logs tab):
Debug logs on the Developer Console
3. Now that the trigger is in place, don’t close the Developer Console window. Instead, jump to the CRM and create a new opportunity (or update an existing one) with a negative amount:
Trigger error for negative amount value
4. Now save the opportunity with a positive value (let’s say $1,000) and then try to update the amount with $400 (which is lower than 50% of the original value):
Trigger error for business rule on the amount field