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
aks0011aks0011 

How Can I Write a Logic to for Bulk Delete Trigger ?

Hi,

I'm writing a delete trigger and that is bulkify too. I'm not getting the way to prevent valid data of being delete. like for example if I have 10 record to delete and 2 of them are valid or supposed not to be deleted by anyone. so my use case is those 2 recoreds should will not be deleted . and rest all (8) can be deleted easily.

so if anyone knows about it then please let me know. thanks in advance.
Shyam BhundiaShyam Bhundia
Hi,

Basically, you have to go through all the items and the ones you DO NOT want to delete add an error to it using addError

for(Account a : trigger.new){
  if(check not to delete){
     a.addError('You cannot delete this');
  }
}


using the addError will prevent that record from being deleted
Rajendra ORajendra O

Above trigger is correct to prevent record deletion but not sure how you are deleting the records, if it's delete list<sobject>; then I believe you will need to change that statment to Database.delete(list<sobject>, false);

The second param indicate if any record fails to delete skip that and delete rest.

Please see this for more detail : https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_database.htm#apex_System_Database_delete
 

AshlekhAshlekh
Hi,

How will you go to delete a record by delete button on detail page or going to delete a lot of record by first query them and the perform delete operation.

1) You are going to delete a single reocrd by delete button the the above code is correct.

2) if you are going ot detele records by query then first and then perform delete opporation then you have to query only that records which need to delete and pass your validation for deletion.

3) You can also run a batch in which you can query only that record which need to delete and delete them if you want to delete in bulk records.

IF it helps you than please mark it as a solution and ENJOY APEX 
aks0011aks0011
Hi there ...,
thanks for the replies .... :)
Rajendra O.... I think that approach will work i havn't implimented that but need to do some discussion.
to use this... should I filter my records in two different lists.... like one is for .. valid records and one is for invalid records.
but the problem is .. when a delete trigger gets fired it don't hold ... so my doubt is.. will this database.delete(invalidRecordList, false) , allow me to delete randomly...?
Rajendra ORajendra O
You will need to follow two steps.
First create the trigger as Shyam suggested, then in your apex delete call use database.delete, you can use single list to delete. As we have trigger in place, it may add error for any/all records, so database.delete operation will allow us to partially delete records from the list. After dml you can check which records can't be deleted using SaveResult : https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_database_saveresult.htm
shiv@SFDCshiv@SFDC
aks,

Delete bulkify trigger doing the operation using delete a list. so we need to prevent our valid records to add in this list.

Like:
List<sObjetct> recordsToDelete = new List<sObject>();
for(yourObjectName rec : Trigger.new)
{
    if(rec not a valid record)
    {
        recordsToDelete.add(rec);
     }
     delete recordsToDelete ;
}

Vijaya Kumar RegantiVijaya Kumar Reganti
Hi,

Trigger.new can not be used in the for Delete trigger event.

You have to use trigger.old instead.

Thanks,
Vijay
Vijaya Kumar RegantiVijaya Kumar Reganti
Hi,

use the code something like this.

for(Account a :trigger.old){

for(Account acc: [Select Id from Account WHERE Id =: Trigger.oldMap.keySet()]){

  if(chek for validity){

   acc.addError('This Record can not be deleted..!');
 
       }
   }
}
Vijaya Kumar RegantiVijaya Kumar Reganti
Hi,

to add the error, use the following methodology instead.
Instead of this:
acc.addError('This Record can not be deleted..!');

you can use:

for(Account a :trigger.old){

for(Account acc: [Select Id from Account WHERE Id =: Trigger.oldMap.keySet()]){

  if(chek for validity){

   trigger.oldmap.get(acc.Id).addError('This record can not be deleted..!');
 
       }
   }
}

 
grant roshangrant roshan

hello guys,

i am new to saleforce.

can some one give me a example for bulk triggers (after delete and before delete).

and how to execute it or its output screen.

thanks in advance.