SFDC Stop - Always the latest about Salesforce


Full Tutorial Series with videos, free apps, live sessions, salesforce consulting and much more.


Telegram logo   Join our Telegram Channel

Sunday, 24 October 2021

System.DmlException: Insert/Upsert failed. First exception on row 1; first error: INVALID_FIELD, Cannot specify both an external ID reference Parent and a salesforce id, ParentId: []

Hello Trailblazers,


In this post we're going to see the solution of the error "System.DmlException: Insert failed. First exception on row 1; first error: INVALID_FIELD, Cannot specify both an external ID reference Parent and a salesforce id, ParentId: []" OR "System.DmlException: Upsert failed. First exception on row 1; first error: INVALID_FIELD, Cannot specify both an external ID reference Parent and a salesforce id, ParentId: []"


The most common scenario for this error is when we're trying to insert both the parent and child records at the same time and we're providing both - the Salesforce ID of the parent object and the parent object record reference with External ID, mentioned in the child record. Ideally we should only provide one out of Salesforce ID of parent object or parent object reference with External ID but not both. Let's have a look at an example.


Inserting Account and Contact record together

You must be aware of the fact that we can use external ids to create a parent-child relationship automatically while inserting new records in salesforce. For ex: In case of account and contact, you don't need to insert account and then get it's record id to insert contact, in order to link it with earlier inserted account record. You can leverage external id on account to insert both records in a single transaction. Let's see how:

As you can see in the above snippet, I've created a list of sObjects in which I have added one account record and one contact record. I have created an external id field on account with API name as: Reference_ID__c. In case of contact record, I have referred to the parent account record by populating the relationship field "Account" (will be Field_Name__r in case of custom object) with an instance of account object consisting of external id only. Using this we're telling salesforce that this contact is linked to the account with reference id as 123. The above code snippet worked perfectly fine upon execution and as a result, account and contact records are created in salesforce. Give it a try!!

Let's do a very small change now:

As you can see above, now we've also specified AccountId in contact record and that too as null which probably should have no impact. Let's execute the code and see what we get:


As you can see we're getting the same error System.DmlException: Upsert failed. First exception on row 1; first error: INVALID_FIELD, Cannot specify both an external ID reference Account and a salesforce id, AccountId: []

As per the post heading error message: "System.DmlException: Insert/Upsert failed. First exception on row 1; first error: INVALID_FIELD, Cannot specify both an external ID reference Parent and a salesforce id, ParentId: []" 

Here our Parent is Account and ParentId is AccountId these can be different depending upon your field name and what objects you're dealing with. This simply means:

"Whenever we're performing a DML on child records, we cannot specify both the parent reference record, as well as the parent record id together, even if the parent record id is null"

So, make sure you're only specifying one of these. That's all for this tutorial everyone, I hope you liked it. Let me know your feedback in the comments down below.

Happy Trailblazing!!

Saturday, 2 October 2021

First error: Exceeded max size limit of 6000000 OR Exceeded max size limit of 12000000 - Solution

Hello Trailblazers, in this post we're going to talk about the resolution of a very common error as given below:

First error: Exceeded max size limit of 12000000 OR First error: Exceeded max size limit of 6000000 

This error appears when you've exceeded the heap size limit in a single transaction. The heap size limit for a synchronous transaction is 6MB and for an asynchronous transaction is 12MB. Asynchronous transactions basically consists of batch classes, future methods and queueable apex.

How to resolve this error?

You need to reduce the heap size in order to resolve this error. Heap size is basically the memory that your code is taking up while executing. It is the sum of all the memory your variables, maps, sets, lists or any other data structure is consuming. In order to resolve the error you need to reduce the heap size. Below are some tips to help you out:

1. Use Limits.getHeapSize() to understand how much heap size (approx) is used in the current transaction upto a particular point.

2. Use Limits.getLimitHeapSize() to get the total amout of heap size available in the current transaction.

3. Use SOQL For Loops, if you use SOQL for loops the result of the query is given in the batch of 200 records while using a list variable or a single record is returned while using a single instance variable. In both the cases, lesser memory is consumed as we're only storing a maximum 200 records at a time (compared to 50,000 records queried in a single SOQL statement).
List<Account> accounts = [SELECT Id FROM Account];
// Stores 50,000 records at max - greater heap size
for(List<Account> accounts : [SELECT Id FROM Account]) {
}
// Stores 200 records at max - lesser heap size

4. Manage your code effectively using helper functions - Avoid class level variables.
void callMe()
{
    List<Account> accounts = [SELECT Id FROM Account];
    System.debug(Limits.getHeapSize()); // Coming as: 321085
}
callme();
System.debug(Limits.getHeapSize()); // Coming as: 1044

As you can see above, the heap size is reduced after the function call is complete because the call stack is now empty and the local variables are destroyed.

5. Clear the lists/map/set/variables which are not in use or make them null



These are some of the tips that can be used to prevent the heap size limit error. Make sure to write effective code when working with large data. If you have any other suggestion, do comment it down below and I'll include that in the blog post. 

That's all for this tutorial everyone I hope you liked it, the full code used in this post can be found here. Do let me know your feedback/thoughts in the comments down below.

Sharing a Salesforce Help Article related to this error: https://help.salesforce.com/s/articleView?id=000321537&type=1

Happy Trailblazing!!