+ Start a Discussion
SFDC DummySFDC Dummy 

How to create trigger for updating field based on picklist

Hi Friends

Now i have created two object masterTest and bankbooktest


in mastertest object i have created field like

Type---Bank Account an party account
Mastercode
master name
Opening balance
Closing balance


In bankbookTest object i have created field like

date
BankAccount(which is masterdetails with masterTest and putting filter if type==bank Account)
Party Code (which is lookup with masterTest and putting filter if type==Party Account)
Payment Type
Amount

I am creating some record manualy in masterTest with respect to Type

when i am creating BankbookTest record ,i am selecting date ,bankaccount ,payment type ,party code,amount,after save it will update the closing balance of masterTest

If payment type==payment/receipt  then it will update closing balance of mastertest if type==bank Account,   closing balance will be opening balance-total payment+total reciept)
If payment type==payment/receipt then it will update closing balance of mastertest if type==Party Account,   closing balance will be opening balance+total payment-total reciept)
Best Answer chosen by SFDC Dummy
Abhishek BansalAbhishek Bansal
Hi,

Please find the required trigger below :
 
trigger updateMasterRecords on BankBookTest__c (after insert, after update) {
    List<MasterTest__c> allMasterRecords = new List<MasterTest__c>([Select Opening_Balance__c, Closing_Balance__c, Date__c, Type__c from MasterTest__c]);
    
    for(BankBookTest__c bankBook : trigger.new){
        for(MasterTest__c master : allMasterRecords){
            if(master.id == bankBook.BankAccount__c){
                if(master.Type__c == 'Bank Account'){
                    if(bankBook.Transaction_Type__c == 'Payment'){
                        if(master.Closing_Balance__c != null){
                            master.Closing_Balance__c = master.Closing_Balance__c - bankBook.Amount__c;
                        }
                        else{
                            master.Closing_Balance__c = master.Opening_Balance__c - bankBook.Amount__c;
                        }
                    }
                    else if(bankBook.Transaction_Type__c == 'Reciept'){
                        if(master.Closing_Balance__c != null){
                            master.Closing_Balance__c = master.Closing_Balance__c + bankBook.Amount__c;
                        }
                        else{
                            master.Closing_Balance__c = master.Opening_Balance__c + bankBook.Amount__c;
                        }
                    }
                }
                else if(master.Type__c == 'Party Account'){
                    if(bankBook.Transaction_Type__c == 'Payment'){
                        if(master.Closing_Balance__c != null){
                            master.Closing_Balance__c = master.Closing_Balance__c + bankBook.Amount__c;
                        }
                        else{
                            master.Closing_Balance__c = master.Opening_Balance__c + bankBook.Amount__c;
                        }
                    }
                    else if(bankBook.Transaction_Type__c == 'Reciept'){
                        if(master.Closing_Balance__c != null){
                            master.Closing_Balance__c = master.Closing_Balance__c - bankBook.Amount__c;
                        }
                        else{
                            master.Closing_Balance__c = master.Opening_Balance__c - bankBook.Amount__c;
                        }
                    }
                }
            }
            if(master.id == bankBook.Party_Code__c){
                if(master.Type__c == 'Bank Account'){
                    if(bankBook.Transaction_Type__c == 'Payment'){
                        if(master.Closing_Balance__c != null){
                            master.Closing_Balance__c = master.Closing_Balance__c - bankBook.Amount__c;
                        }
                        else{
                            master.Closing_Balance__c = master.Opening_Balance__c - bankBook.Amount__c;
                        }
                    }
                    else if(bankBook.Transaction_Type__c == 'Reciept'){
                        if(master.Closing_Balance__c != null){
                            master.Closing_Balance__c = master.Closing_Balance__c + bankBook.Amount__c;
                        }
                        else{
                            master.Closing_Balance__c = master.Opening_Balance__c + bankBook.Amount__c;
                        }
                    }
                }
                else if(master.Type__c == 'Party Account'){
                    if(bankBook.Transaction_Type__c == 'Payment'){
                        if(master.Closing_Balance__c != null){
                            master.Closing_Balance__c = master.Closing_Balance__c + bankBook.Amount__c;
                        }
                        else{
                            master.Closing_Balance__c = master.Opening_Balance__c + bankBook.Amount__c;
                        }
                    }
                    else if(bankBook.Transaction_Type__c == 'Reciept'){
                        if(master.Closing_Balance__c != null){
                            master.Closing_Balance__c = master.Closing_Balance__c - bankBook.Amount__c;
                        }
                        else{
                            master.Closing_Balance__c = master.Opening_Balance__c - bankBook.Amount__c;
                        }
                    }
                }
            }
            master.Date__c = bankBook.Date__c;
        } 
        update allMasterRecords;
    }
}

Let me know if there is any issue.

Thanks,
Abhishek