Hey guys, today in this post we are going to learn about How to write a apex trigger on Contact, whenever Contact record created and if LeadSource Field is having value as ‘Web’ or ‘Phone Inquiry’ then Populate Level as ‘Primary’ automatically.
Final Output →
Other related post that would you like to learn in Salesforce
Create Apex Trigger →
Step 1:- Create Apex Controller : ContactTrigger.apxt
SFDX:Create Apex Trigger>> New >> ContactTrigger.apxt
ContactTrigger.apxt [Apex Trigger]
TRIGGER ContactTrigger ON Contact (BEFORE INSERT) {
ContactTriggerHandler.lavelPopulate(TRIGGER.new);
}
Create Apex Class Controller
Step 2:- Create TriggerHandler Class : ContactTriggerHandler.apxc
From Developer Console >> File >> New >> Apex Class
ContactTriggerHandler.apxc [Apex Class Controller]
public class ContactTriggerHandler {
public static void lavelPopulate(List<Contact> contactList){
FOR(Contact con:contactList){
IF(con.LeadSource == 'Web' || con.LeadSource == 'Phone Inquiry'){
con.Level__c='Primary';
}
}
}
}
Lead Source Field
Lavel Field
Further post that would you like to learn in Salesforce
What are the two options for when apex triggers can run?
There are two types of Apex triggers in Salesforce: Before Triggers and After Triggers. Before Triggers execute before the record is saved to the database, while After Triggers execute after the record has been saved.
Can apex trigger make a callout?
Even though there's the restriction of not allowing API callouts from triggers, there's a workaround to make it happen. You can use Asynchronous Apex to make callouts.
What is the limit of apex triggers in Salesforce?
Your Apex triggers (combined) must not exceed 200 SOQL queries per batch. If they do, your Clean job for that object fails. In addition, if your triggers call future methods, they're subject to a limit of 10 future calls per batch.
Related Topics | You May Also Like
Our Free Courses →
👉 Get Free Course →
📌 Salesforce Administrators 📌 Salesforce Lightning Flow Builder 📌 Salesforce Record Trigger Flow Builder |
👉 Get Free Course →
📌 Aura Lightning Framework 📌 Lightning Web Component (LWC) 📌 Rest APIs Integration |