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
JeffStevensJeffStevens 

Mass Delete

I'm trying to write a class that will delete all records in a custom object with a specific status.  Can't seem to get pass the syntax.

 

I've got this...

 

public with sharing class DeletePostedToHistory{

 

List<CustomObj__c> CustomObj = [

select status__c  

from CustomObj__c  

where status__c = 'Posted to History'];  

 

delete CustomObj;

 

}

 

But I get the error "expecting right curly bracket, found 'delete'

 

What am I missing here?

 

Thanks,



 

 

Best Answer chosen by Admin (Salesforce Developers) 
Eager-2-LearnEager-2-Learn

I am going to take a stab at this but I am still continously learning myself.

 

It looks like you are trying to execute SOQL from with the class itself.  Would you need something like this?

 

public with sharing class DeletePostedToHistory{
 
   public void DeleteIt() {
      List<CustomObj__c> CustomObj = [ select status__c  
                               from CustomObj__c 
                               where status__c = 'Posted to History']; 
      delete CustomObj;
   }
}

 

 

 

All Answers

Eager-2-LearnEager-2-Learn

I am going to take a stab at this but I am still continously learning myself.

 

It looks like you are trying to execute SOQL from with the class itself.  Would you need something like this?

 

public with sharing class DeletePostedToHistory{
 
   public void DeleteIt() {
      List<CustomObj__c> CustomObj = [ select status__c  
                               from CustomObj__c 
                               where status__c = 'Posted to History']; 
      delete CustomObj;
   }
}

 

 

 

This was selected as the best answer
Cory CowgillCory Cowgill

A few things:

 

1. In Apex Trigger or Visualforce PAge, you can oly retrieve 50000 records in SOQL calls, AND you can only perform a DML operation on 10000 records.

 

2. If you need to process more than that, I'd suggest either using Apex Data Loader OR you can write an Apex Batch Job to preform a mass operation over all records in the system.

 

Other than that, this below should work. Note you need to select the ID, because that is what a delete operation is going to run off of.

 

public void DeleteIt()

{

      List<CustomObj__c> deleteObjs = [select id, status__c from CustomObj__c where Status__c = 'Posted to History' limit 10000];

      delete deleteObjs;
}

 

OnurKOnurK

Actually you can add a loop. For example if you have 10000 records to delete, you could say:

 

Integer i = 0; i <= 10; i++

 

Then you can delete 10000 records.

NiketNiket

Hi Jeff,

 

You are getting this error because you have pasted your code directly to your class.

 

I would suggest you to write your code in a method and than call the method from somewhere.

 

If I am able to answer your query , please mark it as a solution.

 

 

Cheers !

Niket

Certified Administrator | Certified Developer

MyBlog

JeffStevensJeffStevens

Thanks so much everyone!