Hey guys, today in this post we are going to learn about how to Write a trigger on Account Whenever New Account Record is created, then needs to create associated Contact Record Automatically with Account name as Contact LastName and Account Phone as Contact Phone in Salesforce.
What is the use of Apex triggers?
Apex triggers enable you to perform custom actions before or after events to records in Salesforce, such as insertions, updates, or deletions. Just like database systems support triggers, Apex provides trigger support for managing records.
Final Output →
You can download file directly from github by Click Here.
Other related post that would you like to learn in Salesforce
- Find the below steps ▾
Create Apex Trigger →
Step 1:- Create Apex Trigger : accountContactTrigger.apxt
From Developer Console >> File >> New >> Apex Trigger
accountContactTrigger.apxt [Apex Trigger]
TRIGGER accountContactTrigger ON Account (BEFORE INSERT, after INSERT) {
IF(TRIGGER.isBefore){
system.debug('I am inside before event');
}ELSE IF(TRIGGER.isAfter){
IF(TRIGGER.isInsert){
List<Contact> conList = NEW List<Contact>();
FOR(Account a1:TRIGGER.new){
Contact c1 = NEW Contact();
c1.accountId=a1.Id;
c1.LastName=a1.Name;
c1.phone=a1.Phone;
conList.add(c1);
}
INSERT conList;
}
}
}
Further post that would you like to learn in Salesforce
What is an apex trigger?
Apex triggers enable you to perform custom actions before or after changes to Salesforce records, such as insertions, updates, or deletions. A trigger is Apex code that executes before or after the following types of operations: insert. update.
What is the use of Apex triggers?
Apex triggers enable you to perform custom actions before or after events to records in Salesforce, such as insertions, updates, or deletions. Just like database systems support triggers, Apex provides trigger support for managing records.
Can we write two triggers on same object?
Writing multiple triggers renders the system unable to recognize the order of execution. Moreover, each trigger that is invoked does not get its own governor limits. Instead, all code that is processed, including the additional triggers, share those available resources.
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 |