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 | Apex Trigger Create Related Contact whenever new Account is created in Salesforce

3,383 views

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 β†’

Trigger Create Related Contact whenever new Account -- w3web.net

 

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]

  1.    TRIGGER accountContactTrigger ON Account (BEFORE INSERT, after INSERT) {    
  2.   IF(TRIGGER.isBefore){
  3. 		system.debug('I am inside before event');
  4.  
  5. 	}ELSE IF(TRIGGER.isAfter){
  6. 		IF(TRIGGER.isInsert){
  7. 			List<Contact> conList = NEW List<Contact>();
  8. 			FOR(Account a1:TRIGGER.new){
  9. 				Contact c1 = NEW Contact();
  10. 				c1.accountId=a1.Id;
  11. 				c1.LastName=a1.Name;
  12. 				c1.phone=a1.Phone;		
  13.                	conList.add(c1);			
  14. 			}
  15. 			INSERT conList;
  16. 		}
  17. 	}
  18. }

 
trigger Create Related Contact whenever new Account is created -- w3web.net

Further post that would you like to learn in Salesforce

 

 

FAQ (Frequently Asked Questions)

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 β†’
 
 


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