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
Somya TiwariSomya Tiwari 

Internal Salesforce.com Error in while inserting records of Master - Detail Object

I have a scenario where I need to insert more than 60 detail records of a single master. In the same instance I have to insert the master record get the ID and then insert 60 different details of the same.
The same works flawlessly until 38 records here is the limits that are utilized in inserting 38 records:
17:08:53.1 (641222913)|SYSTEM_METHOD_ENTRY|[91]|Database.insert(List<SObject>) 
17:08:53.1 (641260235)|DML_BEGIN|[91]|Op:Insert|Type:Payment_Schedules__c|Rows:38 
17:08:53.1 (641283908)|LIMIT_USAGE|[91]|DML|2|150 
17:08:53.1 (641291826)|LIMIT_USAGE|[91]|DML_ROWS|39|10000 
17:08:53.1 (641308456)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:156 
17:08:53.1 (739617623)|DML_END|[91]
17:08:53.1 (739747119)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:156 
17:08:53.1 (739787515)|SYSTEM_METHOD_EXIT|[91]|Database.insert(List<SObject>) 
17:08:53.1 (739800747)|STATEMENT_EXECUTE|[92] 
17:08:53.1 (739814918)|LIMIT_USAGE|[92]|SCRIPT_STATEMENTS|6645|200000 
17:08:53.1 (739881176)|HEAP_ALLOCATE|[92]|Bytes:8 
17:08:53.1 (739900982)|DML_BEGIN|[92]|Op:Update|Type:Lending_Contract__c|Rows:1 
17:08:53.1 (739919823)|LIMIT_USAGE|[92]|DML|3|150 
17:08:53.1 (739930181)|LIMIT_USAGE|[92]|DML_ROWS|40|10000 
17:08:53.1 (739947279)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:8 
17:08:53.1 (754589970)|DML_END|[92] 17:08:53.1 (754609159)|STATEMENT_EXECUTE|[93] 
17:08:53.1 (754620238)|LIMIT_USAGE|[93]|SCRIPT_STATEMENTS|6646|200000 
17:08:53.1 (754936104)|VARIABLE_ASSIGNMENT|[93]|Calculations.LendingContractID|"a004x000001d9BhAAI" 
17:08:53.1 (754968258)|METHOD_EXIT|[1]|01p4x000003WgvL|Calculations.CreateSchedules(Double, Double, Integer, String, Date, Date) 
17:08:53.1 (754980981)|SYSTEM_MODE_EXIT|false 
17:08:53.764 (764240185)|CUMULATIVE_LIMIT_USAGE 
17:08:53.764 (764240185)|LIMIT_USAGE_FOR_NS|(default)| 

Number of SOQL queries: 0 out of 100 
Number of query rows: 0 out of 50000 
Number of SOSL queries: 0 out of 20 
Number of DML statements: 3 out of 150 
Number of Publish Immediate DML: 0 out of 150 
Number of DML rows: 40 out of 10000 
Maximum CPU time: 189 out of 10000 
Maximum heap size: 0 out of 6000000 
Number of callouts: 0 out of 100
Number of Email Invocations: 0 out of 10 
Number of future calls: 0 out of 50 
Number of queueable jobs added to the queue: 0 out of 50
Number of Mobile Apex push calls: 0 out of 10 

17:08:53.764 (764240185)|TOTAL_EMAIL_RECIPIENTS_QUEUED|0
The same code fails when there are more than 38 records here is the limits usage when tried for 39 records:
17:08:58.1 (588262540)|SYSTEM_METHOD_ENTRY|[91]|Database.insert(List<SObject>) 17:08:58.1 (588307190)|DML_BEGIN|[91]|Op:Insert|Type:Payment_Schedules__c|Rows:39 
17:08:58.1 (588331920)|LIMIT_USAGE|[91]|DML|2|150 
17:08:58.1 (588340141)|LIMIT_USAGE|[91]|DML_ROWS|40|10000 
17:08:58.1 (588356694)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:160 
17:08:58.1 (610899136)|DML_END|[91] 
17:08:58.1 (615098680)|SYSTEM_MODE_EXIT|false 
17:08:58.1 (615455839)|FATAL_ERROR|Internal Salesforce.com Error 
17:08:58.615 (615492715)|CUMULATIVE_LIMIT_USAGE 
17:08:58.615 (615492715)|LIMIT_USAGE_FOR_NS|(default)| 

Number of SOQL queries: 0 out of 100 
Number of query rows: 0 out of 50000 
Number of SOSL queries: 0 out of 20 
Number of DML statements: 2 out of 150 
Number of Publish Immediate DML: 0 out of 150 
Number of DML rows: 40 out of 10000
Maximum CPU time: 175 out of 10000 
Maximum heap size: 0 out of 6000000 
Number of callouts: 0 out of 100 
Number of Email Invocations: 0 out of 10
Number of future calls: 0 out of 50 
Number of queueable jobs added to the queue: 0 out of 50 
Number of Mobile Apex push calls: 0 out of 10 

17:08:58.615 (615492715)|TOTAL_EMAIL_RECIPIENTS_QUEUED|0
Do we have any kind of fix how we can solve the same... The code is quite simple with the following insert and update patterns
  • Insert MasterRecord
  • for loop to create details:
  • Update detailRecordList
  • Update MasterRecord
ShirishaShirisha (Salesforce Developers) 
Hi Sowmya,

Greetings!

Unfortunately, "Internal Server Errors" are VERY difficult to debug.  They typically involve a call into Salesforce to look up a "Gack" (only a few internal Salesforce employees have the ability do do this).  Even when they're able to lookup the Gack, it's not always evident what the root cause is.

 I would suggest you to start with stripping out much of your batch class and then start adding pieces back in one by one into your batch class, so that you can narrow down the culprit.

Kindly mark it as best answer if it helps so that it can help others in the future.

Warm Regards,
Shirisha Pathuri