function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Steve Fouracre 16Steve Fouracre 16 

Link Account and Contact

Im pretty sure you can do this and I think Ive done before but I can t exactly remember how.
I want to link Account and Contact together but I dont know the Id of the Account. Similarly to how Workbench uses Smart Lookup I want to eneter the Name of the Account and the DML does the rest to link the 2 objects

So it would be something like 

Contact c = new Contact (Firstname='Steve', Lastname='Hendry', AccountId='36 Robinsmith Rd');
insert c  ;

As you see Ive not provided the Id for the AccountId but so long as the Account '36 Robinsmith Rd' exists I want them to link
Santosh Sriram 2310Santosh Sriram 2310
Hi Steve

You can do this if you use an external ID field on Account to associate it with. The external ID works good if its unique.
Hope this helps!

Santosh
Steve Fouracre 16Steve Fouracre 16
Hi Santosh

You cant do that because the Account on the Contact is already a standard field

Steve
Santosh Sriram 2310Santosh Sriram 2310
Hi Steve, 

This is what i mean when I say external ID field. 

1. Create an unique external ID field on account
2. Associated that to a contact
 
// Create the parent record reference.
// An account with this external ID value already exists.
// This sObject is used only for foreign key reference
// and doesn't contain any other fields.
Account accountReference = new Account(
    MyExtID__c='SAP111111');                

//now your contact code
Contact c = new contact(firstname = 'Steve', Lastname = 'Hendry', Account = accountReference);
insert c;

Hope this helps! If its does, kindly mark this as the solution.