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
srikanthpallasrikanthpalla 

delete trigger senario

HI, any one can help to write trigger

I have amount field on custom object.i want write trigger for senario when i want to delete the all records where amount>10000 and also list the IDs of all deleted records
Best Answer chosen by srikanthpalla
Ajay K DubediAjay K Dubedi
Hi srikanthpalla,
First of all you should use script to delete all the records in which amount > 10000.
Script :
List<CustomObject__c> objList = new List<CustomObject__c>();
objList = [SELECT ID FROM CustomObject__c WHERE Amount__c > 10000];
system.debug('List of Id'+objList);
delete objList;
If you want to use trigger, use this :
 
trigger toDeleteRecords on CustomObject__c (before update) {
List<CustomObject__c> objList = new List<CustomObject__c>();
set<Id> setofobjectId = new set<Id>();
for(CustomObject__c co : trigger.new){
if(co.Amount__c > 10000){
setofobjectId .add(co.Id);
}
}
objList = [SELECT ID FROM CustomObject__c WHERE Id In :setofobjectId];
system.debug('All records to be deleted'+setofobjectId);
delete objList;
}
Regards,
Ajay





 

All Answers

Ajay K DubediAjay K Dubedi
Hi srikanthpalla,
First of all you should use script to delete all the records in which amount > 10000.
Script :
List<CustomObject__c> objList = new List<CustomObject__c>();
objList = [SELECT ID FROM CustomObject__c WHERE Amount__c > 10000];
system.debug('List of Id'+objList);
delete objList;
If you want to use trigger, use this :
 
trigger toDeleteRecords on CustomObject__c (before update) {
List<CustomObject__c> objList = new List<CustomObject__c>();
set<Id> setofobjectId = new set<Id>();
for(CustomObject__c co : trigger.new){
if(co.Amount__c > 10000){
setofobjectId .add(co.Id);
}
}
objList = [SELECT ID FROM CustomObject__c WHERE Id In :setofobjectId];
system.debug('All records to be deleted'+setofobjectId);
delete objList;
}
Regards,
Ajay





 
This was selected as the best answer
@Karanraj@Karanraj
The trigger is an event based action it will execute only if there is any action in that object like insert, update or delete.
In your case if you want to delete all records where the amount is >10000 then my suggestion is to write a batch class and execute the class from executing anonymous window or run using the scheduled apex class.
Through batch class, you can able to handle more number of records

To know more about batch class check this link.https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_batch_interface.htm
global class deleteRecords implements Database.Batchable<sObject>,Database.Stateful{

  global List<Opportunity> optyRecords = new List<Opportunity>();

   global Database.QueryLocator start(Database.BatchableContext BC){
      return Database.getQueryLocator('Select Id,Amount from Opportunity where Amount >10000');
   }

   global void execute(Database.BatchableContext BC, List<sObject> scope){
     optyRecords = (List<Opportunity>)scope;
     delete optyRecords;
    }

   global void finish(Database.BatchableContext BC){
    
    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
    mail.setToAddresses(new String[] {'EmailId'});
    mail.setReplyTo('Email ID');
    mail.setSenderDisplayName('Batch Processing');
    mail.setSubject('Batch Process Completed');
    mail.setPlainTextBody('Batch'+ optyRecords);
    Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
       
   }
}