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
AbAb 

Create Account and contact in one go for test class

Hello,

How is it possible to create a account and contact details in one go

Account accTmp = new account(Name='Something, (new contact(Name = something))
Best Answer chosen by Ab
V V Satyanarayana MaddipatiV V Satyanarayana Maddipati
Hello

Create an external id field in both Account and Contact object . Use same external id value for account and contact to refer the particular account. Try to implement the below code :
 
Sobject [] sobj = new List<Sobject>();

Account acc = new Account(Name='Hallie1', External_Id__c='SAP111111');
contact con = new Contact(lastName='Test Lastname1', Account = new Account(External_Id__c='SAP111111'));
sobj.add(acc);
sobj.add(con);
 Database.SaveResult[] results = Database.insert(sobj);

Make sure the parent sObject must precede the child sObject in the array(sobj), that is, the array index of the parent must be lower than the child’s index.

Further Information, follow this link : https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_dml_foreign_keys.htm

Thanks
Satya.