+ 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