Hey guys, today in this post we are going to learn Trigger Scenario: – When an Account record is created then Create a related Contact automatically in Salesforce.
Final Output β
Other related post that would you like to learn in Salesforce
Create Apex Trigger β
Step 1:- Create Apex Controller : AccountTrigger.apxt
SFDX:Create Apex Trigger>> New >> AccountTrigger.apxt
AccountTrigger.apxt [Apex Trigger]
![]() |
TRIGGER AccountTrigger ON Account (BEFORE INSERT, after INSERT) {
IF(TRIGGER.isBefore){
}ELSE IF(TRIGGER.isAfter && TRIGGER.isInsert){
accountTriggerHandler.contactRelAccount(TRIGGER.new);
}
}
Create Trigger Handler Controller β
Step 2:- Create Apex Controller : accountTriggerHandler.apxc
SFDX:Create Apex Trigger>> New >> accountTriggerHandler.apxc
accountTriggerHandler.apxc [Trigger Handler]
![]() |
public class accountTriggerHandler {
public static void contactRelAccount(List<Account> accList){
List<Contact> conList = NEW List<Contact>();
FOR(Account acc:accList){
Contact con= NEW Contact();
con.FirstName=acc.Name;
con.LastName='Con Last Name';
con.Phone=acc.Phone;
con.Description=acc.Description;
con.AccountId=acc.Id;
conList.add(con);
}
IF(!conList.isEmpty()){
INSERT conList;
}
}
}
Further post that would you like to learn in Salesforce
What is the difference between Contacts and related Contacts?
The 'Related Contacts' list for an Organization is a list of all Contacts whose 'Organization Name' field matches the Organization in question.
What is the difference between Contacts related and contact roles?
A contact is someone directly associated with an account. For example, Kate Chamberlain is the Director of Event Planning for Portsmouth College, so she is listed in the Contacts related list on that account. A contact role, however, describes the part a person plays on the account.
Can a contact be related to multiple accounts in Salesforce?
When you use Contacts to Multiple Accounts, each contact still requires a primary account (the account in the Account Name field). The contact and its primary account have a direct relationship. But you can add other accounts to the contact. These secondary account-contact relationships are indirect.
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 |