0

There are two objects student_details__c and department__c. department__c - contains name and no_of_students__c; student_details__c contains name, department(lookup to department object).

when a student is inserted in a department, that respective department needs to increment the no_of_students__field,similarly when department is changed of a student from one dept to other then no_of_students__c needs to be incremented in the new department and decremented from the old department field. no_of_student__c is the total count of students in respect to the department the belong to in each department object.

There maybe other ways to accomplish this,but need to create a trigger for this at the moment.

I dont know why it is giving me error,it is

Trigger

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

    studentchangeshandler studobj = new studentchangeshandler();
    if(trigger.isinsert)
    {
    studobj.afterinsert(trigger.new);
    }

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

Trigger Handler

public class studentchangeshandler{



         public void afterupdate(student_details__c[] newstudList , map<id,sobject> oldstudlist) {

         set<id> Deptids = new set<id>();

         for(student_Details__c record: newstudlist){

            Deptids.add(record.Department__c);
            }


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


     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;
                   }

            }       

            for(department__c dept : deptlist){       
                  for(Student_details__c student : dept.student_details__r){
        Student_details__c oldStud = (student_details__c)Trigger.oldMap.get(student.ID);





             //student_details__c oldstud = (student_details__c)oldstudlist.get(dept.ID);
                       if (oldstud.department__c == '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;   

 }



}
}

getting error

Error: Invalid Data. Review all error messages below to correct your data. Apex trigger Deptnumberupdate caused an unexpected exception, contact your administrator: Deptnumberupdate: execution of AfterUpdate caused by: System.NullPointerException: Attempt to de-reference a null object: Class.studentchangeshandler.afterupdate: line 89, column 1 at line student_details__c oldstud = (student_details__c)Trigger.oldMap.get(dept.ID);

5

Trigger.oldMap is Map of student_details__c object and not department__c object. So, you are trying to fetch wrong value.

Also, this map is returning a null value which you are trying to typecast to student_details__c object.

  • I am trying to get the student_detail__c record which is related to the department__c.how could I make this method work? – Niknit Jul 14 '17 at 12:29
5

Problem is with this line:

student_details__c oldstud = (student_details__c)Trigger.oldMap.get(dept.ID);

Trigger.oldMap has mapping with the id of a Student_details__c and you're trying to get it with dept.Id. This will return null since department Id's are different from Student_details__c id.

Your problem can be solve like this:

// Iterate related list of Student associated deptList 
for(department__c department:deptlist) {
    for(Student_details__c student : department.student_details__r){
        Student_details__c oldStudent = (student_details__c)Trigger.oldMap.get(student.ID);
    }
}

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.