Create Projects
Quick Start with Apex Development
Apex Tools
Use Anonymous Apex
Test Apex Code
Refactor Apex Code
Aura
Visualforce
Use Custom Code Templates
Complete this quick start to create, test, and debug a workflow that is created using an Apex class, and sets off an Apex trigger.
We create a custom object called Book, and add a custom field called Price to that custom object. We update and test the updated Price field using Apex and learn how to work with Apex code using the Salesforce Extensions.
Book for the label.Books for the plural label.Number for the data type and click Next.Price for the field label.16 in the length text box.2 in the decimal places text box, and click Next.Book_c object. Click the retrieve icon to run SFDX: Retrieve Source from Org.MyBooks.applyDiscount to this class.Make this method both public and static. Because it’s a static method, you don’t need to create an instance of the class to access the method. Use the name of the class followed by a dot (.) and the name of the method.
1public with sharing class MyBooks {
2 public MyBooks() {
3
4 }
5 public static void applyDiscount(Book__c[] books) {
6 for (Book__c b : books){
7 b.Price__c *= 0.9;
8 }
9 }
10}This method takes one parameter, a list of Book records, which is assigned to the variable books. The method iterates through a list of books and applies a 10% discount to the current book price.
Next, add a trigger that calls this applyDiscount method.
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 database. Every trigger runs with a set of context variables that provide access to the records that caused the trigger to fire.
MyBooksTrigger.1trigger MyBooksTrigger on Book__c (before insert) {
2
3 Book__c[] books = Trigger.new;
4
5 MyBooks.applyDiscount(books);
6}We now have the code that is needed to update the price of all books that get inserted.
Next, let’s add a test class and add a unit test. Unit tests are an important part of writing code and are required. In addition to ensuring the quality of your code, unit tests enable you to meet the code coverage requirements for deploying or packaging Apex. To deploy Apex or package it for AppExchange, unit tests must cover at least 75% of your Apex code, and those tests must pass.
Now, add a test class with one test method. Then run the test and verify code coverage. The test method exercises and validates the code in the trigger and class. Also, it enables you to reach 100% code coverage for the trigger and class. For our example, create a test class that inserts a new book object that sets off the Apex trigger we wrote earlier.
To create the test class:
MyBooksTestClass.MyBooksTestClass.cls file:1@isTest
2private with sharing class MyBooksTestClass {
3 static testMethod void validateMyBooks() {
4 Book__c b = new Book__c(Name='Behind the Cloud', Price__c=100);
5 // Insert book
6 insert as user b;
7 // Retrieve the new book
8 Book b2 = [SELECT Price__c FROM Book__c WHERE Id =:b.Id WITH USER_MODE];
9 //Confirm that the price has been updated correctly
10 Assert.areEqual(90, b2.Price__c);
11 }
12}This test class is defined using the @isTest annotation. We recommend that classes defined this way only contain test methods and any methods required to support those test methods. One advantage to creating a separate class for testing is that classes defined with @isTest don’t count against your org’s limit of 6 MB of Apex code. You can also add the @isTest annotation to individual methods. For more information, see isTest Annotation and Execution Governors and Limits.
Important
MyBooksTestClass file.