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
Santosh Borfalkar 7Santosh Borfalkar 7 

Please help me with the homework

/*Write a trigger that creates two identical Contacts whenever an Account is created. Make sure both Contacts are associated with the Account. Use any values for the fields on the Contacts - just make sure to use variables when populating the fields of each Contact to make sure they are identical.*/

I am not able to associate contacts with Account , this is my code

trigger IdenticalContact on Account (before insert) {          for (Account a : trigger.new)     {         List<contact> cont = new List<contact> ();         for (integer i=0 ; i<2; i++)         {             contact newContact            = new contact();             newContact.AccountId          = a.id;             newContact.LastName           = 'kritpa';             newContact.FirstName          = 'ptName';             cont.add(newContact);         }      insert cont;     }



 
Best Answer chosen by Santosh Borfalkar 7
GovindarajGovindaraj
Hi Santosh,

Try below trigger,
trigger IdenticalContact on Account (after insert) {   
    List<contact> cont = new List<contact>();       
    for (Account a : trigger.new)   {                        
        for (integer i=0 ; i<2; i++)    {             
            contact newContact = new contact();             
            newContact.AccountId = a.id;             
            newContact.LastName = 'kritpa';            
            newContact.FirstName = 'ptName';  
            newContact.email = 'a@a.com';          
            cont.add(newContact);         
            }      
        insert cont;  
   }
}
As it's mentioned Contact should be created after Account is inserted so we should go for 'After insert'.
One more tip, Whenever we use before trigger we don't to mention any DML statement like insert,update,etc

Please let us know if this helps.

Thanks,
Govindaraj.S

All Answers

GovindarajGovindaraj
Hi Santosh,

Try below trigger,
trigger IdenticalContact on Account (after insert) {   
    List<contact> cont = new List<contact>();       
    for (Account a : trigger.new)   {                        
        for (integer i=0 ; i<2; i++)    {             
            contact newContact = new contact();             
            newContact.AccountId = a.id;             
            newContact.LastName = 'kritpa';            
            newContact.FirstName = 'ptName';  
            newContact.email = 'a@a.com';          
            cont.add(newContact);         
            }      
        insert cont;  
   }
}
As it's mentioned Contact should be created after Account is inserted so we should go for 'After insert'.
One more tip, Whenever we use before trigger we don't to mention any DML statement like insert,update,etc

Please let us know if this helps.

Thanks,
Govindaraj.S
This was selected as the best answer
Santosh Borfalkar 7Santosh Borfalkar 7
Thanks for tips and prompt reply. 
 
Jim RhodesJim Rhodes
I am using this https://domyhomework.co.uk/do-my-math-homework service when I need help with my homework. You can use it too!
smammadov2017smammadov2017

Hi all,

I just wanted to provide an alternative solution to the problem above in case our fellow aspiring developers have not covered all of the course material yet:

 

trigger IdenticalContacts on Account (after insert) {
    
    // Create a List that will contain the Contacts that are being created
    
    List<Contact> newContacts   = new List<Contact>();
    
    for (Account acc : Trigger.new) {
        
        // Create variables to store field value information
        String firstName        = 'ptName';        
        String lastName         = 'kritpa';
        String accountId        =  acc.Id;
        
        // Create your contacts and assign values to certain fields 
             
        Contact firstContact    = new Contact();
        firstContact.FirstName  = firstName; 
        firstContact.LastName   = lastName;
        firstContact.AccountId  = accountId;

        Contact secondContact   = new Contact();
        secondContact.FirstName = firstName; 
        secondContact.LastName  = lastName;
        secondContact.AccountId = accountId;
        
        // Add these contacts to the List
        
        newContacts.add(firstContact);
        newContacts.add(secondContact);
        
    }
    
        insert newContacts;
}
 

And below is the test class that will provide 100% code coverage for the trigger above. All we need to do is insert an account:

 

@isTest  
	 public class IdenticalContactsTest {  
	 
         @isTest static void createAccount() {  
	        Account acc       = new Account();  
	        acc.Name   = 'Salesforce';  
            insert acc;  
	    }  
	  
	}

I hope this helps.

Cheers,

Seyran
Martha HartMartha Hart
Your trigger is on the right track, but there's a small issue in the way you're associating the contacts with the account. You need to set the AccountId field of each contact to the Id of the account you're creating contacts for. Here's your modified trigger with the necessary changes:
trigger IdenticalContact on Account (after insert) {
    List<Contact> contList = new List<Contact>();
    
    for (Account acc : Trigger.new) {
        // Create two identical contacts for each new Account
        for (Integer i = 0; i < 2; i++) {
            Contact newContact = new Contact();
            newContact.AccountId = acc.Id;  // Set the AccountId to the current Account's Id
            newContact.LastName = 'Last Name';  // You can use any values you prefer
            newContact.FirstName = 'First Name';  // You can use any values you prefer
            contList.add(newContact);
        }
    }
    
    insert contList;  // Insert the list of contacts associated with the new accounts
}
If you still need help with your homework, feel free to visit the site https://stemfixer.com/

Here are the key changes I made:
  1. Changed the trigger event from before insert to after insert. It's better to create the contacts after the Account is inserted to ensure the Account has a valid Id.
  2. Corrected the assignment of the AccountId field to acc.Id, where acc is the current Account being processed.
With these modifications, the trigger should create two identical contacts for each newly created Account and associate them correctly.