You need to sign in to do that
Don't have an account?
SUGURU PUNDAREEKA
Help: System.NullPointerException: Attempt to de-reference a null object: while creating trigger to increment/decrement field on related object
Hello, I'm hoping someone can help me out!
I am trying to write a trigger on Borrowed_book__c to update a field on a related object Member__c, linked through a lookup field. I am created picklist in trigger object and number data type field on related object (Active_Books__c).
Now I have written the code based on the condition below;
increment or decrement a number field (Active_Books__c) on the related object.
If Borrowed_book__c.Status_of_books__c = Is taking, Active_Books__c needs to be the previous value + 1.
If Borrowed_book__c.Status_of_books__c = Is giving, Active_Books__c needs to be the previous value -1. But i got an error like (execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object). I have question marks at those places in the code.
trigger BorrowerBookStatusTrigger on Borrowed_book__c (after insert, after update) {
{
Map<Id,Id> MappingToBbMap = new Map<Id,Id>();
for(Borrowed_book__c Bb : trigger.new)
MappingToBbMap.put(Bb.Member_ID__c,Bb.Id);
List<Member__c> MembersToUpdate = new List<Member__c>();
for (Member__c m: [SELECT Id,Active_Books__c FROM Member__c WHERE Id IN: MappingToBbMap.keySet()])
{
Id Bb2Id = MappingToBbMap.get(m.Id);
Borrowed_book__c Bb2 = trigger.newMap.get(Bb2Id);
if (Bb2.Status_of_books__c =='Is taking'){
m.Active_Books__c = m.Active_Books__c + 1 ;
MembersToUpdate.add(m);
}else if (Bb2.Status_of_books__c == 'Is giving') {
m.Active_Books__c = m.Active_Books__c - 1;
MembersToUpdate.add(m);
}
}
if( !MembersToUpdate.isEmpty())
Database.update(MembersToUpdate);
}
}
I am trying to write a trigger on Borrowed_book__c to update a field on a related object Member__c, linked through a lookup field. I am created picklist in trigger object and number data type field on related object (Active_Books__c).
Now I have written the code based on the condition below;
increment or decrement a number field (Active_Books__c) on the related object.
If Borrowed_book__c.Status_of_books__c = Is taking, Active_Books__c needs to be the previous value + 1.
If Borrowed_book__c.Status_of_books__c = Is giving, Active_Books__c needs to be the previous value -1. But i got an error like (execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object). I have question marks at those places in the code.
trigger BorrowerBookStatusTrigger on Borrowed_book__c (after insert, after update) {
{
Map<Id,Id> MappingToBbMap = new Map<Id,Id>();
for(Borrowed_book__c Bb : trigger.new)
MappingToBbMap.put(Bb.Member_ID__c,Bb.Id);
List<Member__c> MembersToUpdate = new List<Member__c>();
for (Member__c m: [SELECT Id,Active_Books__c FROM Member__c WHERE Id IN: MappingToBbMap.keySet()])
{
Id Bb2Id = MappingToBbMap.get(m.Id);
Borrowed_book__c Bb2 = trigger.newMap.get(Bb2Id);
if (Bb2.Status_of_books__c =='Is taking'){
m.Active_Books__c = m.Active_Books__c + 1 ;
MembersToUpdate.add(m);
}else if (Bb2.Status_of_books__c == 'Is giving') {
m.Active_Books__c = m.Active_Books__c - 1;
MembersToUpdate.add(m);
}
}
if( !MembersToUpdate.isEmpty())
Database.update(MembersToUpdate);
}
}
Hey Suguru,
When are you getting this error? While Editing the record or creating a new one ?
Thanks,
Priya Ranjan
I would suggest to create a relationship between object - Member (Master) and Borrowed Books (Details). Then convert the Active Book field on Member object to roll-up summary with type as count and filter condition to check if field - Is Taking has value as true.
As per my understanding, for this requirement this would be the simplest design approach. If this answer helps you, please mark it as answered.
If you still need to achieve it via trigger, please let me know so that we will check it further.
Best Regards,
Sasidharan A
sfdcsasid@blogspot.com
1.trigger (above trigger code)
2. To make a junction object and to write a trigger and a trigger handler
Please Assist me which process is make simple and effective