I have some code that was being called from a trigger on a case that, in certain situations, would create add an account and contact to the case. I originally tried to do it synchronously and while it would run without errors, it wouldn't add the contact or account. When I modified the code to run asynchronously in a future call (with no change to the logic except specifying the update of the case!), it worked in the UI, adding the account and contact, but failed unit tests with the error:
System.DmlException: Update failed. First exception on row 0 with id 50021000000wSokAAE; first error: INVALID_FIELD, Cannot specify both an external ID reference Contact and a salesforce id, ContactId: []
List<sObject> toInsert = new List<sObject>();
List<Case> toUpdate = new List<case>();
for (case c: cases){
Account a = new Account();
a.name = c.Account_Name_If_not_in_Salesforce__c;
a.Master_ID__c = util.getRandomString(20);
toInsert.add(a);
Contact con = new Contact();
con.firstname = c.Contact_First_Name__c;
con.lastname = c.Contact_Last_Name__c;
con.Account = new Account(Master_ID__c = a.Master_ID__c);
con.Master_ID__c = string.valueof(a.Master_ID__c);
toinsert.add(con);
c.Account = new Account(Master_ID__c = a.Master_ID__c);
c.contact = new Contact(Master_ID__c = con.Master_ID__c);
toUpdate.add(c);
}
database.insert(toInsert, false);
database.update(toupdate);
Has anyone else found an issue to a similar issue or is this just on of the quirks of the platform?
Best Answer
You need to set the
AccountId
andContactId
values, notAccount
andContact
. In addition, you need to wait until after you insert them to set the relationship.To do so, you're probably going to need to maintain a map of
Case
id toMaster_Id__c
. In addition, using a combined list for insert is going to work poorly for you if you have more than 5 records. More on that later.In regards to why you want to maintain separate lists, take a look at Things You Should Know about Data in Apex.
You can only create 10 separate insert chunks, and every time you alternate creates a new chunk. Consider the following list, which counts for 12 chunks instead of 2: