Home  >  Blog  >   Salesforce

How to call APEX Class

Rating: 5
  
 
11092
  1. Share:
Salesforce Articles

We have several ways to perform the above action, they are:

  • Anonymous window.
  • Trigger.
  • Process builder.
  • Integration from other platforms like Java, .net.
  • Scheduling code.
  • Javascript button.
  • From another class.
  • From VFP (Visualforce Page).

Want to enhance your skills in dealing with the world's best CRM, enroll in our Salesforce Online Course

Trigger

What is a Trigger?

It is a very powerful facility in Salesforce which we can use in any case. The use is when something happens in Salesforce (Eg: Record inserted / Record edited / Record deleted), and then you want to take some action, then we use Trigger. Triggers are almost the same as WFR (Workflow Rule).

Scenario: 

Client - Levis Company.

Requirement: As it marks the 15th anniversary for Levi’s, they want to give INR 100 flat discount for any jeans purchased above INR 1000, and send SMS to the customer when he/she qualifies for the discount. The discount would be INR 100.

Solution: 

APEX Class

How to create a Trigger in Salesforce?

Prerequisite - A object with “Levi’s” should be created and in it, and 2 fields are required to perform the process flow (Trigger & Apex Class). 

Code insertion in the Object: 

Trigger Code:

Trigger JeanDiscountTrigger on Levis__c(before insert)
{
    JeanClassDemonstration.applyDiscount(Trigger.new);
}

Apex Class Code:

Public Class JeanClassDemonstration
{
    Public static void applyDiscount(listJeanListNew)
{
for(Levis__c j :JeanListNew)
{
               if(j.Price__c >=1000)
{
                    j.Price__c = j.Price__c-100;
}
}
}
}

Steps to create a Trigger:

Copy the code → Object Definition Page →  Find “Trigger” section → New → Paste the code of Trigger → Click On “Quick Save”.

Steps to create Apex Class:

Log in to Salesforce Org → Setup → Build → Develop → Click ‘Apex Class’ → Click On “New” button → Paste the “Code for Apex Class” → Click On “Quick Save”.

Note: Firstly, the Apex Class code should be executed as we are calling it from Trigger. The below-mentioned figure will explain to you in detail.

 

Apexclassgif

Apexclassgif - Gif

Triggergif

Triggergif 

Now, the Trigger is created on the “Levi’s” Object. We will look into the functionality “with discount” and “without discount”. 

Login to Salesforce Org → Sales → Select “Levis” Object → New → Save. 

[Logic - If the jean’s price is more than 1000 or equal to, we will have a discount of 100.]

Triggerwithoutdiscount

Triggerwithdiscount

Now, the Trigger is disabled so that the discount will not apply in any logic. An example is shown in the below figure.

Log in to Salesforce Org → Sales → Select “Levis” Object → New → Save. 

[Logic - If the jean’s price is more than or equal to 1000, we will not have a discount of 100.]

Checkout SalesForce Tutorial

 

Triggerwithoutdiscount

Triggerwithoutdiscount

What are Events in Trigger?

Apex Triggers allows you to perform custom actions after and before changes in Salesforce records for updating, deletion, and insertion. We can divide them into 2 groups, they are:

Group AGroup B
BeforeInsert
AfterUpdate
 Delete
 Undelete.

Based, on the requirement, the events are used in the Trigger. For example, if the requirement is to Before - insert/update, the following code is done:

Code: 

Works only for Insert:

Trigger JeanDiscountTrigger on Levis__c(before insert){}

Works for both Insert / Update:

Trigger JeanDiscountTrigger on Levis__c(before insert, before update){}

How to decide the Combination(Before/After) of the Events to be included in the Trigger?

The combination(Before/After) of the use of the Event is done on the basis - “Before/after record inserts/updates to Database.”

Eg: Before record “inserted into Db”.
After record “inserted into Db”.
Before record “updated to Db”.
After record “updated to Db”. etc

Explanation:

  • Before -If you want business logic to work before the record gets inserted/updated etc.
  • After - If you want business logic to work after the record gets inserted/updated etc.

Note: Which records will be coming into Trigger? The answer would be the records which are recently updated or inserted will come into Trigger. The records on which we don’t perform any action/work won’t come.

Checkout Salesforce Interview Questions

Program#1

Scenario: We have a property list, which includes Villa, Row House, Apartment in which if the buyer opts for “Row House” then he/she should be given 5% discount as per the slow market of row houses.

MindMajix YouTube Channel

Solution:

Pre-requisites: Object with fields price, type.
Type - Villa, Row House, Apartment, etc.
Price - Should be entered during the entry.

Code:

Trigger Code:

Trigger PropertyDiscountTrigger on Property__c (Before Insert){
    RowHouseDiscount.applyDiscount(Trigger.new);
}

Apex Class Code:

Public class RowHouseDiscount{
    Public static void applyDiscount(listNewHouseList){
    for(Property__c r : NewHouseList){
        if(r.Type__c == ‘Row House’){
    r.Price__c = r.Price__c - (r.Price__c * 0.05)
}
}
}
}

In the next topic, we will discuss in detail about “Context Variables In Salesforce”. Keep following us for more info on Salesforce Development / Programming.

Explore Salesforce Sample Resumes! Download & Edit, Get Noticed by Top Employers!Download Now!

Mindmajix offers different Salesforce certification training according to your desire with hands-on experience on Salesforce concepts

Salesforce Administration Training Salesforce Lightning Training 
Salesforce Advanced Developer Training Salesforce Developer Training 
Salesforce IoT Training Salesforce App Builder Certification Training 
Salesforce AppExchange Training Salesforce Service Cloud Training 
and many more.. 
Join our newsletter
inbox

Stay updated with our newsletter, packed with Tutorials, Interview Questions, How-to's, Tips & Tricks, Latest Trends & Updates, and more ➤ Straight to your inbox!

Course Schedule
NameDates
Salesforce TrainingApr 27 to May 12View Details
Salesforce TrainingApr 30 to May 15View Details
Salesforce TrainingMay 04 to May 19View Details
Salesforce TrainingMay 07 to May 22View Details
Last updated: 03 Apr 2023
About Author

Arogyalokesh is a Technical Content Writer and manages content creation on various IT platforms at Mindmajix. He is dedicated to creating useful and engaging content on Salesforce, Blockchain, Docker, SQL Server, Tangle, Jira, and few other technologies. Get in touch with him on LinkedIn and Twitter.

read more
Recommended Courses

1 / 15