You need to sign in to do that
Don't have an account?
Joe Antonecchia
Help with UNKNOWN_EXCEPTION from Scheduled Apex
Hi Everyone and thanks for taking the time to review my issue,
I'm writing my first Scheduled Apex class that will do some processing on updated and deleted rows. I schedule the class to run as a job it errors. If I copy the code (minus the reschedule code at the bottom) and run it in developer console it works. When it runs as a job it errors with the following error in the log:
11:39:25.042 (42229387)|EXCEPTION_THROWN|[5]|System.UnexpectedException: UNKNOWN_EXCEPTION: An unexpected error occurred. Please include this ErrorId if you contact support: 912151193-21775 (1909173740)
11:39:25.042 (42308796)|HEAP_ALLOCATE|[5]|Bytes:133
11:39:25.042 (42419133)|FATAL_ERROR|System.UnexpectedException: UNKNOWN_EXCEPTION: An unexpected error occurred. Please include this ErrorId if you contact support: 912151193-21775 (1909173740)
Class.PSPMultioutput.execute: line 5, column 1
Here is the code:
global class PSPMultioutput implements Schedulable {
global void execute(SchedulableContext SC) {
try {
String tablename = 'Account';
Database.GetUpdatedResult r = Database.getUpdated(tablename, Datetime.now().addMinutes(-2), Datetime.now());
List<Id> updatedIdList = r.getIds();
// sObject types to describe
String[] types = new String[]{tablename};
// Make the describe call
Schema.DescribeSobjectResult[] results = Schema.describeSObjects(types);
DescribeSObjectResult res = results[0];
List<String> fields = new List<String>(res.fields.getMap().keySet());
String qry = 'Select ' + String.join(fields, ',') + ' from ' + tablename;
for (Account acct : Database.query(qry)){
String name = acct.name;
System.debug('Account: ' + name);
}
System.debug('Query succesfully executed.');
} catch (Exception ce) {
System.debug('\nFailed to execute query successfully, error message ' +
'was: \n' + ce.getMessage());
}
//schedule next execution after a minute
DateTime currentDateTime=System.now().addMinutes(1);
String nextScheduleTime=String.valueof(currentDateTime.second()) +' '+String.valueof(currentDateTime.minute())+' '
+String.valueof(currentDateTime.hour())+' '+String.valueof(currentDateTime.day())+' '
+String.valueof(currentDateTime.month())+' ? '+String.valueof(currentDateTime.Year());
PSPMultioutput testObj = new PSPMultioutput();
system.schedule('Scheduled at '+System.now().format(), nextScheduleTime, testObj);
for( CronTrigger cronTriggerItem:[Select Id From CronTrigger where
NextFireTime= null AND State='DELETED' Limit 100])
{
System.abortJob(cronTriggerItem.id);
}
}
}
I'm writing my first Scheduled Apex class that will do some processing on updated and deleted rows. I schedule the class to run as a job it errors. If I copy the code (minus the reschedule code at the bottom) and run it in developer console it works. When it runs as a job it errors with the following error in the log:
11:39:25.042 (42229387)|EXCEPTION_THROWN|[5]|System.UnexpectedException: UNKNOWN_EXCEPTION: An unexpected error occurred. Please include this ErrorId if you contact support: 912151193-21775 (1909173740)
11:39:25.042 (42308796)|HEAP_ALLOCATE|[5]|Bytes:133
11:39:25.042 (42419133)|FATAL_ERROR|System.UnexpectedException: UNKNOWN_EXCEPTION: An unexpected error occurred. Please include this ErrorId if you contact support: 912151193-21775 (1909173740)
Class.PSPMultioutput.execute: line 5, column 1
Here is the code:
global class PSPMultioutput implements Schedulable {
global void execute(SchedulableContext SC) {
try {
String tablename = 'Account';
Database.GetUpdatedResult r = Database.getUpdated(tablename, Datetime.now().addMinutes(-2), Datetime.now());
List<Id> updatedIdList = r.getIds();
// sObject types to describe
String[] types = new String[]{tablename};
// Make the describe call
Schema.DescribeSobjectResult[] results = Schema.describeSObjects(types);
DescribeSObjectResult res = results[0];
List<String> fields = new List<String>(res.fields.getMap().keySet());
String qry = 'Select ' + String.join(fields, ',') + ' from ' + tablename;
for (Account acct : Database.query(qry)){
String name = acct.name;
System.debug('Account: ' + name);
}
System.debug('Query succesfully executed.');
} catch (Exception ce) {
System.debug('\nFailed to execute query successfully, error message ' +
'was: \n' + ce.getMessage());
}
//schedule next execution after a minute
DateTime currentDateTime=System.now().addMinutes(1);
String nextScheduleTime=String.valueof(currentDateTime.second()) +' '+String.valueof(currentDateTime.minute())+' '
+String.valueof(currentDateTime.hour())+' '+String.valueof(currentDateTime.day())+' '
+String.valueof(currentDateTime.month())+' ? '+String.valueof(currentDateTime.Year());
PSPMultioutput testObj = new PSPMultioutput();
system.schedule('Scheduled at '+System.now().format(), nextScheduleTime, testObj);
for( CronTrigger cronTriggerItem:[Select Id From CronTrigger where
NextFireTime= null AND State='DELETED' Limit 100])
{
System.abortJob(cronTriggerItem.id);
}
}
}
method has caused some problems this year I recall. There is a "fixed" report of it being broken here:
https://success.salesforce.com/issues_view?id=a1p30000000T1FnAAK
I think I remember it being a problem for someone at my work through the summer too.
It might be worth following the instructions and logging a case with Salesforce support. It might just be a limitation of the Scheduled Apex harness (especially if you say it works in Anonymous Apex), but an "UNKNOWN EXCEPTION" isn't of any use to anybody really, is it :D