You need to sign in to do that
Don't have an account?

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; }
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; }
Try below trigger, 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
Try below trigger, 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
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:
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:
I hope this helps.
Cheers,
Seyran