2

I'm trying to insert contact and account records in a single DML. In my case I don't want to use the . operator Instead, I'm using the put operator to insert records.

Account acc=new Account(Name='Test Acc 8', External_Id__c='ext123');
Account a = new Account(External_Id__c='ext123');
Contact con = new Contact();
con.put('LastName', 'kk');
con.put('Account',a);

Above code results in error:

System.SObjectException: Invalid field Account for Contact

Although when I use:

Account acc=new Account(Name='Test Acc 8', External_Id__c='ext123');
Account a = new Account(External_Id__c='ext123');
Contact con = new Contact();
con.LastName = 'kk';
con.Account = a;

it works.

PS: External_Id__c is an external id field on Account.

1 Answer 1

3

You need to use putSobject instead of put. The latter only works on normal fields, while the former is used to set relationship objects.

con.putSObject('Account',a);

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .