My code works everywhere except in a particular client sandbox. It basically sends an email on insert of a record.
Pseudo-code in PKG A
trigger on after-insert of recordX
Messaging.sendEmail(email)
Offending code in sandbox
insert new RecordX
The error in this specific sandbox is:
System.EmailException: SendEmail failed. First exception on row 0; first error: UNKNOWN_EXCEPTION, You have uncommitted work pending. Please commit or rollback before calling out: []
Here are the last few lines of the logs:
16:03:13.395 (431553895)|VARIABLE_SCOPE_BEGIN|[39]|mail|Messaging.SingleEmailMessage|true|false
16:03:13.395 (431641669)|VARIABLE_ASSIGNMENT|[39]|mail|"common.api.soap.wsdl.SingleEmailMessage@51484da"|0x1d47ef24
16:03:13.395 (431647105)|STATEMENT_EXECUTE|[40]
16:03:13.395 (431662684)|VARIABLE_SCOPE_BEGIN|[9]|email|Messaging.SingleEmailMessage|true|false
16:03:13.395 (431707578)|VARIABLE_ASSIGNMENT|[9]|email|"common.api.soap.wsdl.SingleEmailMessage@51484da"|0x1d47ef24
16:03:13.395 (431712353)|STATEMENT_EXECUTE|[10]
16:03:13.395 (431761721)|STATEMENT_EXECUTE|[11]
16:03:13.395 (431812208)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:4
16:03:13.395 (431836983)|HEAP_ALLOCATE|[4]|Bytes:5
16:03:13.395 (431853427)|VARIABLE_ASSIGNMENT|[4]|rt|null|
16:03:13.395 (431858129)|STATEMENT_EXECUTE|[14]
16:03:13.395 (431859506)|STATEMENT_EXECUTE|[14]
16:03:13.395 (431887361)|STATEMENT_EXECUTE|[14]
16:03:13.395 (431942623)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:8
16:03:13.395 (585405722)|EXCEPTION_THROWN|[14]|System.EmailException: SendEmail failed. First exception on row 0; first error: UNKNOWN_EXCEPTION, You have uncommitted work pending. Please commit or rollback before calling out: []
16:03:13.395 (586030977)|HEAP_ALLOCATE|[14]|Bytes:163
16:03:13.395 (586074343)|VARIABLE_SCOPE_BEGIN|[16]|e|System.EmailException|true|false
16:03:13.395 (586151005)|VARIABLE_ASSIGNMENT|[16]|e|"common.apex.methods.MessagingStaticMethods$EmailExecutionException: SendEmail failed. First exception on row 0; first error: UNKNOWN_EXCEPTION, You have uncommitted work pending. Please commit or rollback before calling out: []"|0x6406da32
16:03:13.395 (586159529)|STATEMENT_EXECUTE|[16]
16:03:13.395 (586198500)|HEAP_ALLOCATE|[17]|Bytes:159
16:03:13.395 (586206818)|HEAP_ALLOCATE|[17]|Bytes:27
16:03:13.395 (586235313)|STATEMENT_EXECUTE|[17]
16:03:13.395 (586285305)|FATAL_ERROR|System.EmailException: SendEmail failed. First exception on row 0; first error: UNKNOWN_EXCEPTION, You have uncommitted work pending. Please commit or rollback before calling out: []
Trigger.PKGA.TriggerX: line 14, column 1
16:03:13.395 (586302271)|FATAL_ERROR|System.EmailException: SendEmail failed. First exception on row 0; first error: UNKNOWN_EXCEPTION, You have uncommitted work pending. Please commit or rollback before calling out: []
While this KB article seems to address the issue (albeit indirectly, as I am not doing a callout here) I don't understand the reason for the discrepancy.
Note that sending a simple email through Execute Anonymous works fine.