In the previous article, we learned about the basics of Apex Apex – Mortgage Banking Technology | Origination Strategy Consulting (takefiveconsulting.com). This article presents the step by step procedure of creating a simple Apex class and method on a custom object and custom field. The prerequisites can be either one of the following.
- A Salesforce account in a sandbox Professional, Enterprise, Performance, or Unlimited Edition org.
- An account in a Developer org.
The first step is to create a custom object (Here it is Product). This custom object will be updated through a trigger later in the following steps.
- Create a Custom Object and custom field
The Product custom object is created by the declarative way. Custom objects already have some standard fields, like Name and CreatedBy. We can add other fields that are more specific to our implementation. Here a custom field Discount is created for the custom object. This field will be accessed by the Apex class we write in the next step.
- Log in to your sandbox or Developer org.
- Go to Setup-Object Manager-Create custom object- Enter Product for the label-Save.
- Now go to Custom fields and Relationships section of the Product custom object.
- Create a custom field of Number data type and label as Discount-Save
Now a custom object Product and custom field Discount are created. Next step is to add an Apex class that contains a method for updating the product price. This method is later called by the trigger in the next step
2. Adding an Apex Class
Apex code is generally contained in classes. This class is defined as public, which means the class is available to other Apex classes and triggers.
- Go to Setup-enter “Apex Classes” in the Quick Find box-select Apex Classes and click New.
- In the class editor, enter this class definition:
public class Priceupdates{
}
Above written code is the class definition. To this class, we will be adding one method in the next step.
- Adding method to the Apex class
Add the following method definition between the class opening and closing brackets. This method is called applyDiscount.
public class Priceupdates {
public static void applyDiscount(Product__c[] products) {
for (Product__c p :products){
p.Price__c *= 0.5;
}
}
}
This method takes one parameter, a list of Product records, which is assigned to the variable products. __c in the object name Product__c indicates that it is a custom object. Standard objects in the Salesforce don’t end with this postfix.
Similarly Price__c indicates that it is a custom field.
The statement p.Price__c *= 0.5; takes the old value of p.Price__c, multiplies it by 0.5, which means its value will be discounted by 50%, and then stores the new value into the p.Price__c field.
The statement can be also written as p.Price__c = p.Price__c * 0.5;
Click Save to save the new class.
Now the Apex class that contains some code that iterates over a list of products and updates the Price field for each product. This code will be called by the Apex trigger which we should create in the next step. Please click
To learn more about Apex , please click Apex Development Process | Apex Developer Guide | Salesforce Developers
Looking for more information on Apex classes, 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.