You need to sign in to do that
Don't have an account?
What's wrong with the below code???
System.NullPointerException: Attempt to de-reference a null object: Trigger.updateCount: line 13, column 1
trigger updateCount on Student__c (after insert, before delete)
{
//////////////////////////////////////
// When insert/update any student’s in class then update MyCount Accordingly.
// Created 20131009 max
// Modified 20131009
//////////////////////////////////////
if(Trigger.isAfter && Trigger.isInsert)
{
for (Student__c stent : Trigger.new)
{
Id cid = stent.Class__c;
Integer clist = [Select Count() FROM Student__c where Class__c =: cid ];
//stent.Class__r.My_Count__c = clist;
}
}
else if(Trigger.isAfter && Trigger.isDelete)
{
for (Student__c stent : Trigger.new)
{
Id cid = stent.Class__c;
Integer clist = [Select Count() FROM Student__c where Class__c =: cid ];
//stent.Class__r.My_Count__c = clist;
}
}
}
stent.Class__r.My_Count__c
instead of referring to My Count field present in Class__c object.
Id cid = stent.Class__c;
See if the Class__c field value is not empty.
Regards,
Satish Kumar
Satish It returns the ID of class and its not null.
The problem is with this code "stent.Class__r.My_Count__c".
stent is a child object(Student__c) reference, using it I'm trying to access field present in parent object(i.e class__c)
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger updateCount caused an unexpected exception, contact your administrator: updateCount: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.updateCount: line 16, column 1
Help will be appreciated
Max,
I am new to salesforce but I believe there are several issues here. I am considering you would like to maintain the count of the students of a particular class in a class record.
a. Trigger.New is available only in insert and update triggers. And is updateable in before triggers only.
b. You are updating a field in class__c object in the trigger of Student__c, you would need to do a explicit update here. (atleast that what I read, may there is a cool way other than that I am not sure).
c. Each time in the loop you are doing a query to the get a count of the student records for a given class, which we can avoid as it might be unneccessary if say all the records belong to the same class.
d. In the second if you are checking isAfter and isDelete where as you have registered the trigger as before delete. I think that one must be a typo.
Now, If Class is a master of Student (typically it would be) I would rather add a roll-up field for this. But if I had to do it through a trigger I would do something like this.
(* Note just a sample it can be written in much efficient way)