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
Vijay Kumar Rebbala 1Vijay Kumar Rebbala 1 

Error: Compile Error: expecting right curly bracket, found 'try' at line 3 column 4

global class DeleteoldCases{
    List<Case> QueryCases = [SELECT Id FROM Case WHERE Case.CreatedDate < LAST_N_MONTHS:18 ORDER BY CreatedDate ASC  LIMIT 9000];
    try{
    delete QueryCases;
    }catch(DmlException e) {
    System.debug('An unexpected error has occurred: ' + e.getMessage());
    }  
}
Best Answer chosen by Vijay Kumar Rebbala 1
Shashikant SharmaShashikant Sharma
Here you are trying to write try catch block and DML statement directly in the class. You should create a method in class to do so like below code
global class DeleteoldCases{
    List<Case> QueryCases = [SELECT Id FROM Case WHERE Case.CreatedDate < LAST_N_MONTHS:18 ORDER BY CreatedDate ASC  LIMIT 9000];
    
public void deleteCases() {
try{
    delete QueryCases;
    }catch(DmlException e) {
    System.debug('An unexpected error has occurred: ' + e.getMessage());
    }

}
  
}