You need to sign in to do that
Don't have an account?

Very simple before delete trigger - Error
Hi all, I'm trying to run a simple trigger before delete. The error I'm getting is:
System.NullPointerException: Attempt to de-reference a null object
I originally had logic in the loop but removed it to see if it was causing the error. But the error is still showing with this code. The above code works perfectly in insert/update triggers.
System.NullPointerException: Attempt to de-reference a null object
Code:
trigger deleteMultiday on SFDC_Special_Event__c (before delete) { for (SFDC_Special_Event__c co : Trigger.new) { }//end main for }//end trigger
I originally had logic in the loop but removed it to see if it was causing the error. But the error is still showing with this code. The above code works perfectly in insert/update triggers.
All Answers
You can delete a record by using this code :
public static void SupprimerPlayerEtatSupprimer(List<Player__c> players)
{
List<Player__c> playersToDelete = new List<Player__c>();
for(Player__c eachplayers: players)
{
if(eachplayers.Etat__c == 'Suppression Demandée' )
{
Player__c tempPlayer=new Player__c(id=eachplayers.id);
playersToDelete.add(tempPlayer);
}
}
delete playersToDelete;
}
Trigger.new and Trigger are actually actif. You can't delete it. You create a new record with the id. And you delete the ID.
This worked for me StromConsulting but is there any way to exclude System Admin profile? I mean where System Admin can delete the record if needed?
Really it helped me a lot thanks
Get logged in user profile and then depending on this decide the logic
You can get like this
id id1 = userinfo.getProfileId()
Then
select Name from profile where id = :id1;
Allow delete only if profile is Admin.
01trigger T1 on Account (before delete, after delete, after undelete) {
02 if(Trigger.isBefore){
03 if(Trigger.isDelete){
04 if(p.firstRun){
05 Trigger.old[0].addError('Before Account Delete Error');
06 p.firstRun=false;
07 }
08 }
09 }
10}
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_context_variables.htm
if(trigger.isafter && trigger.isdelete)
{
Set<String> setCouriers = new Set<String>();
for ([Object Name] eachCourier : trigger.old)
{
setCouriers.add(eachCourier.Id); // get all the Id's
}
List< [Object Name2] > csCreds = new List<[Object Name2] >(); // for bulk
// passing Id's to another object where data need to be deleted
for ([Object Name2] eachCourier : [SELECT Name From [Object Name2] where Name IN:setCouriers]){
csCreds .add(eachCourier);
}
delete csCourierServiceCreds;
}
}