You need to sign in to do that
Don't have an account?
Chris Marzella
Contact not relating to account
I am trying to create a new contact upon creation of a new account, which works, but it is not relating to the new account, It relates to another account in my dev org. Why is this? I am sure this is something simple enough. I am new to apex and testing out some basic code.
trigger CreateContact on Account (after insert) { for(Account acc : Trigger.new){ Contact c = new Contact(); c.FirstName = 'John'; c.LastName = 'Lock'; c.AccountId = acc.Id; insert c; } }
Is there any trigger written on Contact insertion, that might causing the issue?
Can you please let us know the record type of Account, is it Person record type? In case of person account, contact created itself there is no need to add code for contact creation.
Please verify above points.
Thanks,
Vishal
All Answers
Since you have many contacts with the same name, why don't you do this to confirm:
Thx
Is there any trigger written on Contact insertion, that might causing the issue?
Can you please let us know the record type of Account, is it Person record type? In case of person account, contact created itself there is no need to add code for contact creation.
Please verify above points.
Thanks,
Vishal
You're a genius! lol. I can't believe I didn't check that. Thanks!
Thanks for appreciation, please change the trigger to keep DML operation outside of for loop to ignore the governor limit issue :
trigger CreateContact on Account (after insert)
{
List<Contact> lstContact = new List<<Conatct>();
for(Account acc : Trigger.new)
{
Contact c = new Contact();
c.FirstName = 'John';
c.LastName = 'Lock';
c.AccountId = acc.Id;
lstContact.add(c) ;
}
insert lstContact
}
Thanks,
Vishal