0

When i tried to delete records my trigger generate error of null value.

Trigger

trigger SubscriberUpdateTrigger on Subscriber__c (after Insert, after Update, after delete) {

for(Subscriber__c subs : Trigger.new){           
    if(subs.Active_Subscriber__c == true && subs.Auto_Telemedicine__c == true){

        if(Trigger.isInsert){
            new MDLiveIntegration().contactValue(Trigger.new);    

            if(system.isFuture()) 
               return;

            MDLiveIntegration mdInsert = new MDLiveIntegration();

            MDLiveIntegration.insertSubs(subs.Id);                
        }

        if(Trigger.isDelete){

            new MDLiveIntegration().contactValue(Trigger.new);

            MDLiveIntegration mdInsert = new MDLiveIntegration();
            MDLiveIntegration.deleteSubs(subs.Subscriber_Id__c);    

        }           
    }           
}        
}

get error in line for(Subscriber__c subs : Trigger.new){ of null value. Give me any suggestion.

Thanks Vimal

3 Answers 3

2

Trigger.new Returns a list of the new versions of the sObject records. Note that this sObject list is only available in insert and update triggers, and the records can only be modified in before triggers.

See for more details: https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers_context_variables.htm

So you need to work around the scenario when it is a delete trigger. E.g. use Trigger.Old in that case.

2

You can't use Trigger.New in a delete trigger, there are no 'new' records to work with.

You'll need to cater for the different types:

if(trigger.IsDelete) {
  for(Subscriber__c subs : Trigger.old) {
    // code
  }    
}
else {
  for(Subscriber__c subs : Trigger.new) {
    // code
   }
}

This would be the perfect time to start looking at trigger frameworks, or at least taking the core processing of the trigger and seperating it out to another class that provides a processing method taking a List<Subscriber__c> as a parameter, then you could pass to it trigger.new for an insert/update operation and trigger.old for a delete.

0

Trigger.new is nor available in after triggers. Reason: The record is already updated. So no new thing about it. :) More details can be found here: https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers_context_variables_considerations.htm

By the way, what are you trying to accomplish here?

2
  • Thanks for reply. I tried to insert record using isInsert and Delete record using isDelete but its not working in this condition. i have refer that link but not get any idea. If possible then change in my given code please..
    – Vml
    Commented Nov 5, 2014 at 11:35
  • Could you try using Trigger.old? As below: Commented Nov 5, 2014 at 11:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .