function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Porty 23110Porty 23110 

Trigger that creates contacts when an account is created

I'm new to apex and currently learning variables and trigger. I'm creating simple scenarios and attempting to write triggers for those scenarios. Below are the 2 scenarios I need help with;

Write a trigger that creates a contact when an account is created and should be associated with the account.

Write a trigger that creates two identical Contacts whenever an account is created and should be associated with the account.

Here's the trigger I wrote for the first scenario and I get an error when I try to create a new account. 
How do I go about the second trigger?

trigger AccountCreate on Account (after insert) {
    
    for (Account acc : Trigger.new) {
    
    Contact C = new Contact();
    
    C.LastName = 'Grace';
    C.AccountId = acc.id;
    C.Email = 'testme@hotmail.com';
 
    insert acc;
      }
}
 
Best Answer chosen by Porty 23110
Somya TiwariSomya Tiwari
Hi,

Always remember that you will not use DML statements inside trigger, especially for the Object you are insert. Remember following things:
  1. No Insert in before / after insert.
  2. No Update in Before insert.
  3. No Update in before / after Delete.
  4. No insert in befote / after update
  5. No update in before update.
Here you will need to Insert the contact and hence you will have to use
Insert C;
as per your code. For the code where you need to make two contact for each account use following code.
trigger AccountCreate on Account (after insert) {
    List<Contact> ContoAdd = new List<Contact>();
    for (Account acc : Trigger.new) {
    
    Contact C = new Contact();
    
    C.LastName = 'Grace';
    C.AccountId = acc.id;
    C.Email = 'testme@hotmail.com';
 
    ContoAdd.add(C);
    Contact C1 = new Contact();
    C1.LastName = 'Grace';
    C1.AccountId = acc.id;
    C1.Email = 'testme@hotmail.com';

ContoAdd.add(C1);
      }
insert ContoAdd;
}
Hope the solution helped you in solving your problem. If it did do give a like :)

All Answers

Somya TiwariSomya Tiwari
Hi,

Always remember that you will not use DML statements inside trigger, especially for the Object you are insert. Remember following things:
  1. No Insert in before / after insert.
  2. No Update in Before insert.
  3. No Update in before / after Delete.
  4. No insert in befote / after update
  5. No update in before update.
Here you will need to Insert the contact and hence you will have to use
Insert C;
as per your code. For the code where you need to make two contact for each account use following code.
trigger AccountCreate on Account (after insert) {
    List<Contact> ContoAdd = new List<Contact>();
    for (Account acc : Trigger.new) {
    
    Contact C = new Contact();
    
    C.LastName = 'Grace';
    C.AccountId = acc.id;
    C.Email = 'testme@hotmail.com';
 
    ContoAdd.add(C);
    Contact C1 = new Contact();
    C1.LastName = 'Grace';
    C1.AccountId = acc.id;
    C1.Email = 'testme@hotmail.com';

ContoAdd.add(C1);
      }
insert ContoAdd;
}
Hope the solution helped you in solving your problem. If it did do give a like :)
This was selected as the best answer
Porty 23110Porty 23110
Thank you @Somya Tiwari worked perfectly.