Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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 #22

Open
vijayk3327 opened this issue Aug 27, 2023 · 0 comments
Assignees
Labels
documentation Improvements or additions to documentation question Further information is requested

Comments

@vijayk3327
Copy link
Owner

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. To know more details about Apex Triggers, Click Here →

👉 To get source code live demo link, Click Here.

Create Apex Trigger →
Step 1:- Create Apex Trigger : accountContactTrigger.apxt

`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;
	}
}

}`

👉 To get source code live demo link, Click Here.

@vijayk3327 vijayk3327 added documentation Improvements or additions to documentation question Further information is requested labels Aug 27, 2023
@vijayk3327 vijayk3327 self-assigned this Aug 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation question Further information is requested
Projects
Status: No status
Development

No branches or pull requests

1 participant