You need to sign in to do that
Don't have an account?
AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object:
I am new to salesforce. There is a custom object Lenst which has Master-Details relationship with Standard object Contact I want to write a trigger which updates count field on Contact after there is new record inserted or deleted to Lents object. Here is my code snippet
trigger UpdateCount on Lent__c (after insert) {
List<Contact> counts_toUpdate = new List<Contact>();
if(Trigger.isInsert){
List<Lent__c> counts = [select Contact__r.Count__c
from Lent__c where id IN :Trigger.new FOR UPDATE];
for(Lent__c lent:lentsForContact){
lent.Contact__r.Count__c = lent.Contact__r.Count__c + 1; // Line where I get exception
counts_toUpdate.add(lent.Contact__r);
}
update counts_toUpdate;
}
}
At line 11 I get the following exception.
Error: Invalid data.
Review all error messages below to correct your data.
The trigger Apex UpdateCount caused an unexpected exception. Contact the administrator: UpdateCount: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.UpdateCount: line 11, column 1
Please help me its very urgent for me as I am stuck here from last one day.
Hello,
In the trriger.new you don't have the fields from the master/lookup references.
means: lent.Contact__r.Count__c is null, this why you have this exception.
If you want to use this field, you need first to query it in the code.
However, if the target is just to count number of child records the Contact have, you can use roll-up summery field and save the code writing.
Thanks a lot for your reply. But here I am not able to understand how to query lent.Contact__r.Count__c. In my code I am already using
List<Lent__c> counts = [select Contact__r.Count__c
from Lent__c where id IN :Trigger.new FOR UPDATE];
Can you please help me that how should I get the value for this field.
Thanks in Advance.
So you did query the field.
Sorry, I didn't notice that.
Then maybe the problem is with 'lentsForContact'. What is this value?
shouldn't you use:
for(Lent__c lent : counts){
Sorry It was written mistakenly actual code is this:
trigger UpdateCount on Lent__c (after insert) {
List<Contact> counts_toUpdate = new List<Contact>();
List<Lent__c> counts = [select Contact__r.Count__c
from Lent__c where id IN :Trigger.new FOR UPDATE];
for(Lent__c lent:counts){
lent.Contact__r.Count__c = lent.Contact__r.Count__c + 1;// line 11
counts_toUpdate.add(lent.Contact__r);
}
update counts_toUpdate;
}
And this code was working fine earlier but it is throwing exception at line 11.
Maybe the cound__c current value is indeed null?
In this case, either set the default value in the field to 0 or amend the code:
lent.Contact__r.Count__c = (lent.Contact__r.Count__c==null ? 0 : lent.Contact__r.Count__c) + 1
Hello,
Thanks for the reply. But even after setting the Count field default value to 0 it is throwing same exception. Earlier this code was working very fine and was updating count value after inserting a new record to Lents.
I dont know what happened :( I need it very urgently.
I think I got where the error.
We need to query the Contact in order to update them.
This shuold work.
Thanks a lot for your too quick response. There is an error in compliation as follows:
Error: Compile Error : Incompatible value type Decimal for MAP<String,Integer> à la ligne 9 colonne 1
for(Lent__c lent:counts){
contact_newCount_Map.put(lent.Contact__c, (lent.Contact__r.Count__c==null ? 0 : lent.Contact__r.Count__c) + 1); // line 9
}
Try and play with the types.
I think you're very close to solve it.
maybe:
contact_newCount_Map.put(lent.Contact__c, Integer.valueOf((lent.Contact__r.Count__c==null ? 0 : lent.Contact__r.Count__c) + 1));
Thanks a lot for your code. Now I have to write code for decreasing the count after delete of Lent Object. Will work on it. I hope it will be same like this.
similarly to increment code below code should work in decrement of count after deletion of record on Lent object:
List<Lent__c> counts = [select Contact__c, Contact__r.Count__c
from Lent__c where id IN :Trigger.old FOR UPDATE];
for(Lent__c lent:counts){
contact_newCount_Map.put(lent.Contact__c, Integer.valueOf(lent.Contact__r.Count__c==null ? 0 : lent.Contact__r.Count__c) - 1);
}
}
for(Contact con : [select id, Count__c from Contact WHERE ID IN : contact_newCount_Map.KeySet()]){
con.Count__c=contact_newCount_Map.get(con.id);
counts_toUpdate.add(con);
}
update counts_toUpdate;
}
But the code does not work.
Can you please help me how can I decrease the same count after a record is deleted from Lents object. It is very urgent. Where is the problrm in this code to decrease the value of count after delete is called.
trigger UpdateCount on Lent__c (after insert,after delete) {
List<Contact> counts_toUpdate = new List<Contact>();
Map<String, Integer> contact_newCount_Map=new Map<String, Integer>(); //for each contact the CountNumber
if(trigger.isinsert){
List<Lent__c> counts =[select Contact__c, Contact__r.Count__c
from Lent__c where id IN :Trigger.new FOR UPDATE];
for(Lent__c lent:counts){
contact_newCount_Map.put(lent.Contact__c, Integer.valueOf(lent.Contact__r.Count__c==null ? 0 : lent.Contact__r.Count__c) + 1);
}
}
else if(trigger.isdelete){
Set<Id> contactIds = new Set<Id>();
for (Lent__c l : Trigger.old)
contactIds.add(l.Contact__c);
List<Lent__c> counts = [select Contact__c, Contact__r.Count__c
from Lent__c where id IN :contactIds];
Map<Id,Contact> oldContacts = new Map<Id,Contact>([SELECT Count__c From Contact where Id IN :contactIds]);
for(Lent__c lent : counts)
contact_newCount_map.put(lent.Contact__c, Integer.valueOf(oldContacts.get(lent.Contact__c).Count__c == null ? 0 : oldContacts.get(lent.Contact__c).Count__c ) -1);
}
for(Contact con : [select id, Count__c from Contact WHERE ID IN : contact_newCount_Map.KeySet()]){
con.Count__c=contact_newCount_Map.get(con.id);
counts_toUpdate.add(con);
}
update counts_toUpdate;
}
Please help.
Hi,
Maybe in the delete you cannot query the record (not sure).
Try this