The values of fields in the records in the Trigger.new and Trigger.old collections can be accessed using the dot (.) notation.
Here’s an example that demonstrates how to access field values from the Trigger.new and Trigger.old records:
trigger UpdateContactName on Account (before update) { for (Account a : Trigger.new) { Account oldAccount = Trigger.oldMap.get(a.Id); if (a.Name != oldAccount.Name) { for (Contact c : [SELECT Id, FirstName, LastName FROM Contact WHERE AccountId = :a.Id]) { c.FirstName = a.Name; c.LastName = oldAccount.Name; update c; } } } }
In this code, the Trigger.new is an array of Account records that are being updated. The Trigger.oldMap is a map of IDs to the original versions of the records, before the update.
The code iterates through each account in the Trigger.new array and gets the original account using the Trigger.oldMap.get(a.Id) method, where a.Id is the ID of the current account.
The code then checks if the name of the account has changed by comparing the a.Name and oldAccount.Name values. If the name has changed, the code queries all the contacts associated with the account and updates their FirstName and LastName fields with the new and old account name, respectively.
In this example, the field values are accessed using the dot (.) notation, for example a.Name and oldAccount.Name.