We have several ways to perform the above action, they are:
Want to enhance your skills in dealing with the world's best CRM, enroll in our Salesforce Online Course
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:
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;
}
}
}
}
Copy the code → Object Definition Page → Find “Trigger” section → New → Paste the code of Trigger → Click On “Quick Save”.
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 - Gif
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.]
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.]
Triggerwithoutdiscount
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 A | Group B |
Before | Insert |
After | Update |
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){}
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:
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.
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.
Mindmajix offers different Salesforce certification training according to your desire with hands-on experience on Salesforce concepts
Our work-support plans provide precise options as per your project tasks. Whether you are a newbie or an experienced professional seeking assistance in completing project tasks, we are here with the following plans to meet your custom needs:
Name | Dates | |
---|---|---|
Salesforce Training | Dec 21 to Jan 05 | View Details |
Salesforce Training | Dec 24 to Jan 08 | View Details |
Salesforce Training | Dec 28 to Jan 12 | View Details |
Salesforce Training | Dec 31 to Jan 15 | View Details |
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.