1

I am not able to pass Trigger.old to constructor,

Trigger

trigger Test on Student_Details__c (after insert, after update,after delete) {


            studentupdatehandler studobj = new studentupdatehandler(trigger.new ,trigger.old);
            if(trigger.isinsert)
            {
            studobj.afterinsert();
            }

            if(trigger.isdelete)
            {
            studobj.afterdelete();
            }

           if(trigger.isupdate)
           {
           studobj.afterupdate();
           }    
    }

TriggernHandler

public class studentupdatehandler{

     set<id> newDeptids = new set<id>();  
     set<id> oldDeptids = new set<id>();  


     public studentupdatehandler(student_details__c[] newstudentlist , student_details__c[] oldstudentList){


        for(student_Details__c record: newstudentlist)
        {
          newDeptids.add(record.Department__c);
        }
         for(student_Details__c record: oldstudentlist)
        {
          oldDeptids.add(record.Department__c);
        }



     }



     public void afterinsert() {

        list<department__c> newdeptlist = [select id,name,no_of_students__c,(select id from student_details__r) from department__c where id in:newDeptids];

        toinsert(newdeptlist);


 } 


     public void afterdelete() {

        list<department__c> olddeptlist = [select id,name,no_of_students__c,(select id from student_details__r) from department__c where id in:oldDeptids];

        toupdatedelete(olddeptlist);


 }




    public void afterupdate() {

        list<department__c> newdeptlist = [select id,name,no_of_students__c,(select id from student_details__r) from department__c where id in:newDeptids];
        toinsert(newdeptlist);


        list<department__c> odeptlist= [select id,name,no_of_students__c,(select id from student_details__r) from department__c where id in:oldDeptids];
        toupdatedelete(odeptlist);  

}

Public void toinsert(department__c[] deptlist ){

        for(department__c dept : deptlist){

               if (dept.Name == 'CSE')
                   {
                   dept.no_of_students__c = dept.no_of_students__c +1;
                   }
               else if (dept.Name == 'ECE')
                {
                   dept.no_of_students__c = dept.no_of_students__c +1;
                   }
               else if (dept.Name == 'IT')
                   {
                   dept.no_of_students__c = dept.no_of_students__c +1;
                   }

                else if (dept.Name == 'EEE')
                {
                   dept.no_of_students__c = dept.no_of_students__c +1;
                   }

            }                 
   update deptlist;      
        } 

Public void toupdatedelete(department__c[] odeptlist ){


        for(department__c dept : odeptlist){

               if (dept.Name == 'CSE')
                   {
                   dept.no_of_students__c = dept.no_of_students__c -1;
                    }

                else if (dept.Name == 'ECE')
                {
                   dept.no_of_students__c = dept.no_of_students__c -1;
                   }
                else if (dept.Name == 'IT')
                   {
                   dept.no_of_students__c = dept.no_of_students__c -1;
                   }

                else if (dept.Name == 'EEE')
                {
                   dept.no_of_students__c = dept.no_of_students__c -1;
                   }

            } 



  update odeptlist; 
}

}

The error is on line for(student_Details__c record: oldstudentlist) , the error is

Error: Invalid Data. Review all error messages below to correct your data. Apex trigger Test caused an unexpected exception, contact your administrator: Test: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Class.studentupdatehandler.: line 10, column 1

Can trigger.old not be passed in a constructor?

4

You have one constructor for after insert, after update, after delete actions. But Trigger.old is only available in update and delete triggers. That is why it fails on after insert with null pointer exception.

Refer to documentation

| improve this answer | |
0

try to change your code like this, so that it will not fail on insertion .

 trigger Test on Student_Details__c (after insert, after update,after delete) {
     studentupdatehandler studobj ;
    if(Trigger.isafter && Trigger.isupdate || Trigger.isafter && Trigger.isdelete )         
    studobj = new studentupdatehandler(trigger.new ,trigger.old);
    else
    studobj = new studentupdatehandler(trigger.new , new list<Student_Details__c>());


            if(trigger.isinsert)
            {
            studobj.afterinsert();
            }

            if(trigger.isdelete)
            {
            studobj.afterdelete();
            }

           if(trigger.isupdate)
           {
           studobj.afterupdate();
           }    
    }
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.