You need to sign in to do that
Don't have an account?
Bergin
Binding account to contact
What's wrong in this code?
I'm trying to add contact to a Business account.
public void AddContact(Account account, string firstName, string lastName)
{
Contact NewContact = new Contact();
NewContact.FirstName = firstName;
NewContact.LastName = lastName;
NewContact.Account = account;
System.debug('Before Insert Biz Contact123 : ' + firstName + ' - ' + lastName + ' - ' + account);
//Insert new contact
insert NewContact;
System.debug('After Insert Biz Contact123 : ' + NewContact.FirstName + ' - ' + NewContact.LastName + ' - ' + NewContact.Account.Id);
}
This is what I see in the log, but the account is not bound to the contact.
|USER_DEBUG|[143]|DEBUG|Before Insert Biz Contact123 : James - San - Account:{Name=Jaas, RecordTypeId=012Z0000000D69aIAC, Id=001Z000000k7IA7IAM}
|USER_DEBUG|[150]|DEBUG|After Insert Biz Contact123 : James - San - 001Z000000k7IA7IAM
I'm trying to add contact to a Business account.
public void AddContact(Account account, string firstName, string lastName)
{
Contact NewContact = new Contact();
NewContact.FirstName = firstName;
NewContact.LastName = lastName;
NewContact.Account = account;
System.debug('Before Insert Biz Contact123 : ' + firstName + ' - ' + lastName + ' - ' + account);
//Insert new contact
insert NewContact;
System.debug('After Insert Biz Contact123 : ' + NewContact.FirstName + ' - ' + NewContact.LastName + ' - ' + NewContact.Account.Id);
}
This is what I see in the log, but the account is not bound to the contact.
|USER_DEBUG|[143]|DEBUG|Before Insert Biz Contact123 : James - San - Account:{Name=Jaas, RecordTypeId=012Z0000000D69aIAC, Id=001Z000000k7IA7IAM}
|USER_DEBUG|[150]|DEBUG|After Insert Biz Contact123 : James - San - 001Z000000k7IA7IAM
NewContact.Account refers to the relationship and not the field to be associated on Contact. To associate the account, use the following syntax :
NewContact.AccountId = account.Id;
All Answers
NewContact.Account refers to the relationship and not the field to be associated on Contact. To associate the account, use the following syntax :
NewContact.AccountId = account.Id;