Search This Blog

Wednesday, August 7, 2019

How to link contact with Account in Salesforce


How to link contact with Account in Salesforce

We can use AccountId field of contact to associate contact with particular Account.

check the below link to see the fields of contact object for better idea

https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_contact.htm


Example: Run the following code in Execute anonymous window.

Account objAccount = new Account();
objAccount.Name ='Test Account';
insert objAccount;
system.debug('Account:'+objAccount);
Contact objContact = new Contact();
objContact.lastName ='Last Name';
objContact.firstName = 'First Name';
objContact.AccountId = objAccount.Id;
insert objContact;
system.debug('Contact:'+objContact);


Output: It creates Account and then it associates this Account with the newly created contact.

Happy Coding !!!

No comments:

Post a Comment