+ Start a Discussion
StormConsultingStormConsulting 

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


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.

Best Answer chosen by Admin (Salesforce Developers) 
soofsoof
Trigger.new is not available on before delete, and after delete triggers.  Use Trigger.old instead.  In case you're using the same trigger for insert/ update, which is the case I suppose, then try using Trigger.isInsert, Trigger.isUpdate, and Trigger.isDelete context variables to determine whether to use Trigger.old or Trigger.new.  Hope this makes sense.

All Answers

soofsoof
Trigger.new is not available on before delete, and after delete triggers.  Use Trigger.old instead.  In case you're using the same trigger for insert/ update, which is the case I suppose, then try using Trigger.isInsert, Trigger.isUpdate, and Trigger.isDelete context variables to determine whether to use Trigger.old or Trigger.new.  Hope this makes sense.
This was selected as the best answer
StormConsultingStormConsulting
Thanks a lot!
Axel SalesforceAxel Salesforce

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.

ravikiran.ax1752ravikiran.ax1752

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?

Kamatchi Devi RKamatchi Devi R

Really it helped me a lot thanks

 

Sujit NirkheSujit Nirkhe

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.

Vinod Kumar 231Vinod Kumar 231
Can some one tell me in the below logic, the two anotations "after delete, after undelete" will needed for this logic or not.
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}
Vinod Kumar 231Vinod Kumar 231
What i mean to say is, no use of these two anotaions. Can some one tell me whether my understanding is correct
 
Kevin BrussKevin Bruss
More on trigger context variables here:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_context_variables.htm
sohail najmisohail najmi
trigger  [Trigger Name] on [Object Name] (after delete) {
  
    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;
    }
}