We covered Apex class and method by writting a demo code on a custom object in the previous article Apex Class – Mortgage Banking Technology | Origination Strategy Consulting (takefiveconsulting.com). This code will be called by the Apex Trigger to make changes in a newly inserted record using the method explained in the apex class. It is followed by a test class with one test method to validate the code in the trigger and class.
What is an Apex Trigger?
An apex trigger is a piece of code that executes before or after records of a particular type are inserted, updated, or deleted from the Lightning platform database. Every trigger runs with a set of context variables that provide access to the records that caused the trigger to fire. All triggers run in bulk; that is, they process several records at once.

When we create a record and try to save it, there are several system validation rules which runs to validate the data we entered. These system validation rules are examples of before trigger. After triggers on the other hand run after the record is saved. They are Assignment rules, autoresponse rules, workflow rules etc.
Add an Apex Trigger
Go to Triggers in the object management settings for products, click New.
In the trigger editor, delete the default template code and enter this trigger definition:
trigger PriceupdatesTrigger on Product__c (before insert) {
Product__c[] products = Trigger.new;
Priceupdates.applyDiscount(products);
}
The first line of code defines the trigger including the trigger name, the object on which it operates, and defines the events that cause it to fire. Here, the PriceupdatesTrigger operates on the Product__c object, and runs before new products are inserted into the database.
The second line creates a list of product records named products and assigns it the contents of a trigger context variable called Trigger.new which contains all the new products that are about to be inserted.
The third line calls the method applyDiscount in the Priceupdates Apex class. It passes in the array of new products.
Now the whole code is ready to update the price of a product before it is inserted into the database.
Add a Test Class
Next step is to add a test class with one test method. The test method validates the code in the trigger and class.
@isTest
private class PriceupdatesTestclass {
static testMethod void validatePriceupdates() {
Product__c p = new Product__c(Name=’Product name’, Price__c=100);
System.debug(‘Price before inserting new product: ‘ + p.Price__c);
// Insert product
insert p;
// Retrieve the new product
p = [SELECT Price__c FROM Product__c WHERE Id =:p.Id];
System.debug(‘Price after trigger fired: ‘ + p.Price__c);
// Test that the trigger correctly updated the price
System.assertEquals(50, p.Price__c);
}
}
This is a separate class with isTest annotation. Class defined like this don’t count against the org’s 6MB of Apex code is the test method used.
validatePriceupdates is the test method used. It creates a product and inserts it into the database temporarily. The System.debug statement writes the value of the price in the debug log. insert p inserts the new product, the next line of the code retrieves the newly inserted product using the ID that was initially assigned to the book when it was inserted. The System.debug statement then logs the new price of the product modified by the trigger.
When the Priceupdates class runs, it updates the Price__c field and reduces its value by 50%. The next line of the code verifies that the method applyDiscount ran and produced the expected result.
Now test run the test class and the method in the Developer console.
In this article, we covered the necessary steps to update the price of a product, starting with creation of the custom object Product and custom field price. Then we wrote an Apex class with a method to apply discount to the price. As the next step we saw the Apex trigger which is used to update the price of the newly inserted products by calling the Apex class created in the previous step. Next, we added a test class to validate the Apex trigger and class. After you’ve tested your code, you can deploy the code and any prerequisite components to a production org.
Read more about Apex triggers here Triggers | Apex Developer Guide | Salesforce Developers
Looking for more information on Apex triggers, leave us a message.
Take Five Consulting is a technology company, based in Virginia U.S., that specializes in the Mortgage Banking vertical especially LOS implementation and application development. Take Five Consulting creates and implement mortgage technology and software specifically for Mortgage Industry.