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
Money CareMoney Care 

After Update event not working in my Trigger

Hi All

I have created a trigger for before insert which is fired on the basic of record creation.i need to add before insert and after update event but its not working,can any body find my fault.
trigger setBalance on BankBookTest__c (before insert,after update){
    Set<Id> masterTestIds = new Set<Id>();
    
    for(BankBookTest__c bankBook : trigger.new){
        if(bankBook.BankAccount__c != null){
            masterTestIds.add(bankBook.BankAccount__c);
        }
        if(bankBook.Party_Code__c != null){
            masterTestIds.add(bankBook.Party_Code__c);
        }
    }
    
    Map<Id,MasterTest__c> masterTestRecords = new Map<Id,MasterTest__c>();
    for(MasterTest__c master : [Select Closing_Balance__c from MasterTest__c where id in :masterTestIds]){
        masterTestRecords.put(master.id, master);
    }
     
    for(BankBookTest__c bankBook : trigger.new){
        if(masterTestRecords.containsKey(bankBook.BankAccount__c)){
            bankBook.Bank_Opening_Balance__c = masterTestRecords.get(bankBook.BankAccount__c).Closing_Balance__c;
            if(bankBook.Transaction_Type__c == 'Payment'){
                bankBook.Bank_Closing_Balance__c = masterTestRecords.get(bankBook.BankAccount__c).Closing_Balance__c - bankBook.Amount__c;
            }
            if(bankBook.Transaction_Type__c == 'Reciept'){
                bankBook.Bank_Closing_Balance__c = masterTestRecords.get(bankBook.BankAccount__c).Closing_Balance__c + bankBook.Amount__c;
            }
        }
        
        if(masterTestRecords.containsKey(bankBook.Party_Code__c)){
            bankBook.Party_Opening_Balance__c = masterTestRecords.get(bankBook.Party_Code__c).Closing_Balance__c;
            if(bankBook.Transaction_Type__c == 'Payment'){
                bankBook.Party_Closing_Balance__c = masterTestRecords.get(bankBook.Party_Code__c).Closing_Balance__c + bankBook.Amount__c;
            }
            if(bankBook.Transaction_Type__c == 'Reciept'){
                bankBook.Party_Closing_Balance__c = masterTestRecords.get(bankBook.Party_Code__c).Closing_Balance__c - bankBook.Amount__c;
            }
        }
        
        if(bankBook.Transaction_Type__c == 'Payment'){
            masterTestRecords.get(bankBook.BankAccount__c).Closing_Balance__c = masterTestRecords.get(bankBook.BankAccount__c).Closing_Balance__c - bankBook.Amount__c;
            masterTestRecords.get(bankBook.Party_Code__c).Closing_Balance__c = masterTestRecords.get(bankBook.Party_Code__c).Closing_Balance__c + bankBook.Amount__c;
        }
        if(bankBook.Transaction_Type__c == 'Reciept'){
            masterTestRecords.get(bankBook.BankAccount__c).Closing_Balance__c = masterTestRecords.get(bankBook.BankAccount__c).Closing_Balance__c + bankBook.Amount__c;
            masterTestRecords.get(bankBook.Party_Code__c).Closing_Balance__c = masterTestRecords.get(bankBook.Party_Code__c).Closing_Balance__c - bankBook.Amount__c;
        }
        bankBook.To_DateFUP__c=bankBook.Date__c;
        bankBook.From_DateFUP__c=bankBook.Date__c;
    }
}


 
jenish shingalajenish shingala
In masterTestIds set you are adding an ids of BankAccount__c and Party_Code__c objects. But while doing query (Line 14) you are retreieving records of MasterTest__c objects where id in masterTestIds.

This query always will give null records.

Let me know if this is correct.