Hey guys, today in this post we are going to learn about Write a Apex Trigger on Contact to Create Account Automatically and map to related account Id to Contact Whenever Contact is Created in Salesforce.
Real time scenarios:- Write a Trigger on Contact to Create Account Automatically Along with Name and Phone Number and map to related Account Id to Contact Whenever New Contact is Created.
Files we used in this post example
CreateAutoAccountFromContact.apxt | Apex Class Trigger | It will be fired whenever New Contact is Created |
Parent Cusotm Object:- Account | Parent Object (Account) | Trigger on Child Object (Contact) and Account is Auto created whenever new contact is created |
Child Object:- Contact | Child Object (Contact) | It is child object and associated with Account Object Automatically when new contact is created |
Live Demo
You can download file directly from github by Click Here.
Other related post that would you like to learn in Salesforce
Create Apex Class Trigger Controller
Step 1:- Create Apex Class : CreateAutoAccountFromContact.apxt
From Developer Console >> File >> New >> Apex Class
CreateAutoAccountFromContact.apxt [Apex Class Trigger]
TRIGGER CreateAutoAccountFromContact ON Contact (BEFORE INSERT, after INSERT){
IF(TRIGGER.isBefore){
system.debug('trigger before event');
IF(TRIGGER.isInsert){
List<Contact> listOfContact = NEW List<Contact>();
FOR (Contact conObj : TRIGGER.new)
{
IF (String.isBlank(conObj.accountid))
{
listOfContact.add(conObj);
}
}
system.debug('listOfContact_1 ' + listOfContact);
IF (listOfContact.size() > 0)
{
List<Account> createNewAcc = NEW List<Account>();
Map<String,Contact> conNameKeys = NEW Map<String,Contact>();
FOR (Contact con : listOfContact)
{
String accountName = con.firstname + ' ' + con.lastname;
conNameKeys.put(accountName,con);
Account accObj = NEW Account();
accObj.Name = accountName;
accObj.Phone= con.MobilePhone;
createNewAcc.add(accObj);
}
INSERT createNewAcc;
FOR (Account acc : createNewAcc)
{
system.debug('mapContainsKey ' + conNameKeys.containsKey(acc.Name));
IF (conNameKeys.containsKey(acc.Name))
{
conNameKeys.get(acc.Name).accountId = acc.Id;
}
}
}
}
}
ELSE IF(TRIGGER.isAfter){
system.debug('trigger after event');
}
}
Further post that would you like to learn in Salesforce
What is before insert trigger in Salesforce?
Before insert: When using this event, the code block is executed before a new record is inserted. Before update: When you use this event, the code gets executed before a new record is updated in the object.
What is an apex trigger?
Apex triggers enable you to perform custom actions before or after events to records in Salesforce, such as insertions, updates, or deletions. Triggers can be defined for top-level standard objects, such as Account or Contact, custom objects, and some standard child objects. Triggers are active by default when created.
What are the two options for when Apex triggers can run?
Apex Triggers can either run before a record has been saved of after. A before operation is usually used to verify information that is going to be inserted, and after trigger is used to access data that has previously been entered by a user or system.
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 |