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
Bill McBill Mc 

UNKNOWN_EXCEPTION when deleting cases

Hello.  I have an APEX class that is scheduled for execution every hour. It finds old cases and deletes them.  It has worked for over a year.

Recently, in the last few days, it has started to raise UNKNOWN_EXCEPTION on the line that deletes the cases.

Salesforce support ole me to raise the issue here since I do not have Prem support.

Can anyone help?

Kind regards,  Bill
Kevin CrossKevin Cross
Are you able to post the code?  I suspect that you may be running into scenarios now where you do not have any records that meet the criteria if this has been working but cannot tell for sure without code or more detail on the stack trace of the error.  Therefore, if you cannot share the code, I would recommend trapping exceptions in your code and using System.debug along with getMessage, getStackTraceString, getCause, or other useful exception information (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_exception_methods.htm).  This should hel pyou find the cause yourself but also will give you something to post here.
Bill McBill Mc
Hi Kevin - Thanks for your help.

The debug logs show there are 131 rows.

The APEX code is:

           if(cs.size()>0)
            {
                List<EmailMessage> ems = new List<EmailMessage>();
                List<Task> ts = new List<Task>();
                
                System.Debug('cs.size()=' + cs.size());
                
                // ok, we have the Cases, now find the activities
                // This might return 0 if the Case is older>1 year and the tasks have been archived to ActivityHistory table
                // For those Tasks we will have to bulk delete
                ts = new List<Task>([Select Id from Task where WhoId=null And WhatId IN :cs]);
                
                if(ts.size()>0)
                {
                    System.Debug('ts.size()=' + ts.size());

                    // Great now get the emails
                    ems = new List<EmailMessage>([Select Id from EmailMessage where ActivityId IN :ts]);
                    
                    if(ems.size()>0)
                    {
                        System.Debug('ems.size()=' + ems.size());
                    }
                    else
                    {
                        System.Debug('No Emails Found.');
                    }
                }
                else
                {
                    System.Debug('No Tasks Found.');
                }
                
                // Now reverse delete them (emails, then tasks, then cases)
                delete ems;
                Log('Deleted ' + ems.size() + ' Emails');
                
                delete ts;
                Log('Deleted ' + ts.size() + ' Tasks');
                
                try
                {
                    delete cs;
                    Log('Deleted ' + cs.size() + ' Cases');
                }
                catch(System.DmlException ex)
                {
                    System.Debug('Cause: ' + ex.getCause());
                    System.Debug('Message: ' + ex.getMessage());
                    System.Debug('TypeName: ' + ex.getTypeName());
                    System.Debug('getDmlFieldNames: ' + ex.getDmlFieldNames(0));
                    System.Debug('getDmlFields: ' + ex.getDmlFields(0));
                    System.Debug('getDmlId: ' + ex.getDmlId(0));
                    System.Debug('getDmlMessage: ' + ex.getDmlMessage(0));
                    System.Debug('getDmlType: ' + ex.getDmlType(0));
                    System.Debug('getNumDml: ' + ex.getNumDml());
                }
                
            }

The debug output is:

13:29:16.1 (772234764)|DML_BEGIN|[81]|Op:Delete|Type:Case|Rows:131
13:29:18.3 (2003020988)|DML_END|[81]
13:29:18.3 (2003873005)|EXCEPTION_THROWN|[81]|System.DmlException: Delete failed. First exception on row 0 with id 500D000000zGgs9IAC; first error: UNKNOWN_EXCEPTION, An unknown exception has occurred.: []
13:29:18.3 (2008913917)|USER_DEBUG|[86]|DEBUG|Cause: null
13:29:18.3 (2009085655)|USER_DEBUG|[87]|DEBUG|Message: Delete failed. First exception on row 0 with id 500D000000zGgs9IAC; first error: UNKNOWN_EXCEPTION, An unknown exception has occurred.: []
13:29:18.3 (2009230570)|USER_DEBUG|[88]|DEBUG|TypeName: System.DmlException 13:29:18.3 (2009950885)|USER_DEBUG|[89]|DEBUG|getDmlFieldNames: ()
13:29:18.3 (2010377341)|USER_DEBUG|[90]|DEBUG|getDmlFields: null
13:29:18.3 (2010568592)|USER_DEBUG|[91]|DEBUG|getDmlId: 500D000000zGgs9IAC
13:29:18.3 (2010807407)|USER_DEBUG|[92]|DEBUG|getDmlMessage: An unknown exception has occurred.
13:29:18.3 (2016492287)|USER_DEBUG|[93]|DEBUG|getDmlType: UNKNOWN_EXCEPTION
13:29:18.3 (2016710606)|USER_DEBUG|[94]|DEBUG|getNumDml:
131 13:29:18.3 (2016881373)|SYSTEM_MODE_EXIT|false


 
Kevin CrossKevin Cross
I notice I do not see a second trace for size, so the exception may be coming from the delete on the emails or the tasks.  I would put those inside your IF statements.
if(cs.size()>0)
 {
     List<EmailMessage> ems = new List<EmailMessage>();
     List<Task> ts = new List<Task>();
     
     System.Debug('cs.size()=' + cs.size());
     
     // ok, we have the Cases, now find the activities
     // This might return 0 if the Case is older>1 year and the tasks have been archived to ActivityHistory table
     // For those Tasks we will have to bulk delete
     ts = new List<Task>([Select Id from Task where WhoId=null And WhatId IN :cs]);
     // Now reverse delete them (emails, then tasks, then cases)
     if(ts.size()>0)
     {
         System.Debug('ts.size()=' + ts.size());
         
         // Great now get the emails
         ems = new List<EmailMessage>([Select Id from EmailMessage where ActivityId IN :ts]);
         
         if(ems.size()>0)
         {
             System.Debug('ems.size()=' + ems.size());
             delete ems;
     		 Log('Deleted ' + ems.size() + ' Emails');
         }
         else
         {
             System.Debug('No Emails Found.');
         }
         delete ts;
     	 Log('Deleted ' + ts.size() + ' Tasks');
     }
     else
     {
         System.Debug('No Tasks Found.');
     }
     
     try
     {
         delete cs;
         Log('Deleted ' + cs.size() + ' Cases');
     }
     catch(System.DmlException ex)
     {
         System.Debug('Cause: ' + ex.getCause());
         System.Debug('Message: ' + ex.getMessage());
         System.Debug('TypeName: ' + ex.getTypeName());
         System.Debug('getDmlFieldNames: ' + ex.getDmlFieldNames(0));
         System.Debug('getDmlFields: ' + ex.getDmlFields(0));
         System.Debug('getDmlId: ' + ex.getDmlId(0));
         System.Debug('getDmlMessage: ' + ex.getDmlMessage(0));
         System.Debug('getDmlType: ' + ex.getDmlType(0));
         System.Debug('getNumDml: ' + ex.getNumDml());
     }     
 }

However, I also do not see the trace that says "No Tasks Found." either; therefore, there could be something else off.  Your comment says there may not be any Tasks.  Is it possible that aside from age of the Case that Tasks may exist with WhoId != null ?  The exception could be because you are not finding all the Tasks for the Case and as such cannot delete it due to existing relationships.  What is the intent of that condition?  Should that line be: 
ts = new List<Task>([SELECT Id FROM Task WHERE WhatId IN :cs ALL ROWS]);

Hope that helps.
Bill McBill Mc
Hi Kevin

Sorry, I didn't include all of the log file - just the part after the attempt to delete the cases. The log shows there are 131 of them. (Op:Delete|Type:Case|Rows:131)

It is line #40 (in the code sample in your message) which is causing the exception so that's where I am catching it and printing out the results of some of the methods you suggested.

I dont understand why (and how) we are expected to respond to an "unknown exception". I was told that a Salesforce Developer resource would investigate this - possibly in their own logs to see what the issue - if I posted in this forum.

I dont see enough information to debug this issue and don't really want to go shooting in the dark.

Kind regards,

Bill



 
Kevin CrossKevin Cross
Agreed, which is why I would start with validating the business logic per my post above.  If I need to delete emails, I only should be doing so if there are emails.  Therefore, you need to move that delete and one for tasks under the IFs regardless of if they are causing the issue or not.  It just is better practice.  Similarly, if I am going to delete a Case and its associated Tasks, I think it is incorrect to have another filter on the WhoId because all someone has to do to break the logic is to associate the Task to a Contact or Lead.

P.S. I saw the log showed 131 cases, but I do not see it showing how many Tasks or the message "No Tasks Found."  That may be important. 

P.P.S. The current information has not proven to be more useful, so you can try replacing the catch block with stack trace which may give you some clues.
System.Debug('Stack Trace: ' + ex.getStackTraceString());
Bill McBill Mc
Hi Kevin

I moved the delete of the emails and tasks to inside the IF statements. I also removed the WhoId from the where clause.

In any event, there are no emails or tasks associated with the found cases so there are no attempts to delete emails or tasks.

I added the getStackTraceString to the debugging.  The latest log file is below.  (There is no log statement that no emails were found because it doesnt search for any if no tasks were found.)

08:23:37.1 (348978902)|SOQL_EXECUTE_BEGIN|[35]|Aggregations:0|SELECT Id FROM Case WHERE (SuppliedEmail LIKE '%customer%' AND SuppliedEmail LIKE '%@whizz%' AND AccountId = NULL AND ContactId = NULL AND Status = 'New' AND CreatedDate < :tmpVar1) ORDER BY CreatedDate ASC NULLS FIRST LIMIT 3000
08:23:37.1 (549260528)|SOQL_EXECUTE_END|[35]|Rows:191
08:23:37.1 (551166085)|USER_DEBUG|[44]|DEBUG|cs.size()=191
08:23:37.1 (552056997)|SOQL_EXECUTE_BEGIN|[49]|Aggregations:0|SELECT Id FROM Task WHERE WhatId IN :tmpVar1
08:23:37.1 (562807143)|SOQL_EXECUTE_END|[49]|Rows:0
08:23:37.1 (563127358)|USER_DEBUG|[75]|DEBUG|No Tasks Found.
08:23:37.1 (563241019)|DML_BEGIN|[80]|Op:Delete|Type:Case|Rows:191
08:23:38.769 (1769419911)|DML_END|[80]
08:23:38.769 (1770402806)|EXCEPTION_THROWN|[80]|System.DmlException: Delete failed. First exception on row 0 with id 500D000000zGh9ZIAS; first error: UNKNOWN_EXCEPTION, An unknown exception has occurred.: []
08:23:38.769 (1777223915)|USER_DEBUG|[85]|DEBUG|Cause: null
08:23:38.769 (1777320363)|USER_DEBUG|[86]|DEBUG|Message: Delete failed. First exception on row 0 with id 500D000000zGh9ZIAS; first error: UNKNOWN_EXCEPTION, An unknown exception has occurred.: []
08:23:38.769 (1777391275)|USER_DEBUG|[87]|DEBUG|TypeName: System.DmlException
08:23:38.769 (1778064408)|USER_DEBUG|[88]|DEBUG|getDmlFieldNames: () 08:23:38.769 (1778391559)|USER_DEBUG|[89]|DEBUG|getDmlFields: null
08:23:38.769 (1778525757)|USER_DEBUG|[90]|DEBUG|getDmlId: 500D000000zGh9ZIAS
08:23:38.769 (1778616962)|USER_DEBUG|[91]|DEBUG|getDmlMessage: An unknown exception has occurred.
08:23:38.769 (1782347933)|USER_DEBUG|[92]|DEBUG|getDmlType: UNKNOWN_EXCEPTION
08:23:38.769 (1782472781)|USER_DEBUG|[93]|DEBUG|getNumDml: 191 08:23:38.769 (1782595610)|USER_DEBUG|[94]|DEBUG|Stack Trace: Class.WhizzCaseDeleter.DeleteCases: line 80, column 1 

I really feel quite stuck on how to proceed. Should I not be getting more support from Salesforce?

Bill

 
Kevin CrossKevin Cross
I agree.  It seems this is an unhandled error on the SF side, so I would think they would provide you support on this.  However, I guessing they will provide you said support if you pay for a premium ticket.  I know the line numbers do not match up but if the code is as simple as you are showing here, it definitely suggests something on SF end.  For simple testing purposes, what happens when you delete one of these cases manually?  Does it generate an error?  Are you using the same username and password?  Things to think about while you wait for them as well as could be good information to give to support.
Bill McBill Mc
Hi Kevin

Deleting manually is fine - and it would be the same credentials as my own. 

Do you know if there is more I can do to attract their attention in this forum?

Bill
 
Kevin CrossKevin Cross
No, I'm guessing you will have to open a ticket but may be wrong.  I haven't seen enough posts to know if employees of SF tech support or development team frequents these forums.