Apex Trigger on Contact to Create Account Automatically and map to related account Id to Contact Whenever New Contact is Created in Salesforce | trigger to create account automatically whenever contact is created in salesforce

10,109 views


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

Note:: – You will get an email, so put correct email and mobile number and BEGIN YOUR JOURNEY from Today!
   

Trigger on Contact to create Account Automatically -- w3web.net

 

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]

  1.  
  2.   TRIGGER CreateAutoAccountFromContact ON Contact (BEFORE INSERT, after INSERT){  
  3.     IF(TRIGGER.isBefore){
  4.         system.debug('trigger before event');
  5.         IF(TRIGGER.isInsert){            
  6.  
  7.             List<Contact> listOfContact = NEW List<Contact>();            
  8.  
  9.             FOR (Contact conObj : TRIGGER.new)   
  10.             {  
  11.                 IF (String.isBlank(conObj.accountid))   
  12.                 {  
  13.                     listOfContact.add(conObj);  
  14.                 }  
  15.             }  
  16.             system.debug('listOfContact_1 ' + listOfContact);  
  17.  
  18.             IF (listOfContact.size() > 0)
  19.             {  
  20.                 List<Account> createNewAcc = NEW List<Account>();
  21.                 Map<String,Contact> conNameKeys = NEW Map<String,Contact>();
  22.  
  23.                 FOR (Contact con : listOfContact)   
  24.                 {  
  25.                     String accountName = con.firstname + ' ' + con.lastname;  
  26.                     conNameKeys.put(accountName,con);                      
  27.                     Account accObj = NEW Account();  
  28.                     accObj.Name = accountName;
  29.                     accObj.Phone= con.MobilePhone;
  30.                     createNewAcc.add(accObj);  
  31.                 }  
  32.                 INSERT createNewAcc;  
  33.                 FOR (Account acc : createNewAcc)   
  34.                 {  
  35.                     system.debug('mapContainsKey ' + conNameKeys.containsKey(acc.Name));
  36.  
  37.                     IF (conNameKeys.containsKey(acc.Name))   
  38.                     {  
  39.                         conNameKeys.get(acc.Name).accountId = acc.Id;  
  40.                     }  
  41.                 }  
  42.             } 
  43.         }
  44.     }
  45.     ELSE IF(TRIGGER.isAfter){
  46.         system.debug('trigger after event');
  47.     }
  48.  
  49. }

Trigger on Contact to create Account Automatically - w3web.net

Further post that would you like to learn in Salesforce

Note:: – You will get an email, so put correct email and mobile number and BEGIN YOUR JOURNEY from Today!
   

 


 

FAQ (Frequently Asked Questions)

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

 
Note:: – You will get an email, so put correct email and mobile number and BEGIN YOUR JOURNEY from Today!
 
 
  

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



Hi, This is Vijay Kumar behind the admin and founder of w3web.net. I am a senior software developer and working in MNC company from more than 8 years. I am great fan of technology, configuration, customization & development. Apart of this, I love to write about Blogging in spare time, Working on Mobile & Web application development, Salesforce lightning, Salesforce LWC and Salesforce Integration development in full time. [Read full bio] | | The Sitemap where you can find all published post on w3web.net

Leave a Comment