• SUGURU PUNDAREEKA
  • NEWBIE
  • 0 Points
  • Member since 2022
  • Salesforce Developer
  • LanceSoft, Inc

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 3
    Replies
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
 
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