[SalesForce] Problem in IsInsert and IsDelete in apex trigger

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

Best Answer

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.

Related Topic