function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SUGURU PUNDAREEKASUGURU 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 got this error message
 
PriyaPriya (Salesforce Developers) 

Hey Suguru,

When are you getting this error? While Editing the record or creating a new one ?

Thanks,

Priya Ranjan

SUGURU PUNDAREEKASUGURU PUNDAREEKA
Create/inserting a new record
SasidSasid
Hi Suguru,

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
SUGURU PUNDAREEKASUGURU PUNDAREEKA
I already implemented this solution, but this filter condition makes it such that when I select Is taking value, the value is increased, but when I select Is giving, the value is not decreased. When I choose Is Giving, how do I apply these filters to decrease the value? Please assist me.User-added image
SUGURU PUNDAREEKASUGURU PUNDAREEKA
I tried the filter option, but it didn't work, so I proceeded to the trigger, where I had two choices:
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