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
SFDC DummySFDC Dummy 

After Trigger not saving

Hi Guys


How to create calculation after insert trigger value

after firing the trigger ,how i will created calaculation on particalar value with the help of trigger
trigger realtime2 on BankBook__c (after insert){
List<BankBook__c> bnkBookListToUpdate=new List<BankBook__c>();
   for(BankBook__c bkObj:[SELECT Id,New_Closing_Bal__c,New_Related_Bank_Acc__c,Debit__c,Credit__c  FROM BankBook__c WHERE Id IN :Trigger.new])
   {
   
   double s = bkObj.New_Related_Bank_Acc__c; //copy the notes to a string object
   double s1 = bkObj.Debit__c;
   double s2 = bkObj.Credit__c;                
   
   bkObj.New_Closing_Bal__c=s-s1+s2  ;
       // bkObj.New_Closing_Bal__c= bkObj.New_Related_Bank_Acc__c;
        bnkBookListToUpdate.add(bkObj);
    }
    try{
     update  bnkBookListToUpdate;
    }catch(DMlException de ){
      System.debug(de );
    }
}

error

Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger realtime2 caused an unexpected exception, contact your administrator: realtime2: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.realtime2: line 10, column 1
Sunil PalSunil Pal
Hi SFDC Dummy,

Can you try this 
trigger realtime2 on BankBook__c (After insert){
List<BankBook__c> bnkBookListToUpdate=new List<BankBook__c>();
   for(BankBook__c bkObj: Trigger.new()) {
  
		bkObj.New_Closing_Bal__c = bkObj.New_Related_Bank_Acc__c-bkObj.Debit__c + bkObj.Credit__c  ;
       bnkBookListToUpdate.add(bkObj);
    }
    try{
     update  bnkBookListToUpdate;
    }catch(DMlException de ){
      System.debug(de );
    }
}

Or in Before insert also you can do it will save your dml.

Thanks 
Sunil
SFDC DummySFDC Dummy
i had tried this code what u have share above..but still showing error.. Error: Compile Error: Variable does not exist: Trigger at line 3 column 27
Neetu_BansalNeetu_Bansal
Hi,

For this either you are create a formula field on BankBook object or you can do the calculations on the before insert trigger like this:
As you are updating the same record so it ir preferred to use before insert trigger.

trigger realtim2 on BankBook__c( before insert )
{
    for( BankBook__c b : trigger.new )
    {
        Decimal s = ( b.New_Related_Bank_Acc__c != null ? b.New_Related_Bank_Acc__c : 0.00 );
        Decimal s1 = ( b.Debit__c != null ? b.Debit__c : 0.00 );
        Decimal s2 = ( b.Credit__c != null ? b.Credit__c : 0.00 );
        b.New_Closing_Bal__c = ( s - s1 + s2 );
    }
}

Thanks,
Neetu

 
SFDC DummySFDC Dummy
Again..... Error: Invalid Data. Review all error messages below to correct your data. Apex trigger realtime2 caused an unexpected exception, contact your administrator: realtime2: execution of BeforeInsert caused by: System.NullPointerException: Argument cannot be null.: Trigger.realtime2: line 7, column 1
SFDC DummySFDC Dummy
Hi Neetu Its Ok.. But my requirement is more than S1 and S2 subsrtacted ar adddition at a time....here taking only one time.....how to put its in loop.. trigger realtim2 on BankBook__c(before insert ) { for( BankBook__c b : trigger.new ) { Decimal s = ( b.New_Related_Bank_Acc__c != null ? b.New_Related_Bank_Acc__c : 0.00 ); Decimal s1 = ( b.Debit_formula__c!= null ? b.Debit_formula__c: 0.00 ); Decimal s2 = ( b.Credit_formula__c!= null ? b.Credit_formula__c: 0.00 ); b.New_Closing_Bal__c = ( s - s1 + s2 ); } } Thanks in adv
Neetu_BansalNeetu_Bansal
Have you tried the code, which I pasted?
SFDC DummySFDC Dummy
yes i have tried bellow code trigger realtim2 on BankBook__c(before insert ) { for( BankBook__c b : trigger.new ) { Decimal s = ( b.OB__c!= null ? b.OB__c: 0.00 ); Decimal s1 = ( b.Debit_formula__c!= null ? b.Debit_formula__c: 0.00 ); Decimal s2 = ( b.Credit_formula__c!= null ? b.Credit_formula__c: 0.00 ); b.New_Closing_Bal__c = ( s - s1 + s2 ); } } but here one problem is i have Debit_formula__c more time from my OB__c ,,its taking one time ..calculation
Sunil PalSunil Pal
trigger realtime2 on BankBook__c (After insert){
List<BankBook__c> bnkBookListToUpdate=new List<BankBook__c>();
   for(BankBook__c bkObj: Trigger.new) {
  
		bkObj.New_Closing_Bal__c = bkObj.New_Related_Bank_Acc__c-bkObj.Debit__c + bkObj.Credit__c  ;
       bnkBookListToUpdate.add(bkObj);
    }
    try{
     update  bnkBookListToUpdate;
    }catch(DMlException de ){
      System.debug(de );
    }
}
Please check this once
yogesh_sharmayogesh_sharma
trigger realtime2 on BankBook__c (after insert){
Double S = 0.0;
Double S1 = 0.0;
Double S2 = 0.0;

List<BankBook__c> bnkBookListToUpdate=new List<BankBook__c>();
   for(BankBook__c bkObj:[SELECT Id,New_Closing_Bal__c,New_Related_Bank_Acc__c,Debit__c,Credit__c  FROM BankBook__c WHERE Id IN :Trigger.new])
   {
   S = S + (bkObj.New_Related_Bank_Acc__c!=null ? bkObj.New_Related_Bank_Acc__c : 0.0); //copy the notes to a string object
   S1 = S1 + (bkObj.Debit__c!=null ? bkObj.Debit__c : 0.0);
   S2 = S2 + (bkObj.Credit__c!=null ? bkObj.Credit__c : 0.0);               
   
bkObj.New_Closing_Bal__c=S-S1+S2  ;
       // bkObj.New_Closing_Bal__c= bkObj.New_Related_Bank_Acc__c;
        bnkBookListToUpdate.add(bkObj);
    }
    try{
     update  bnkBookListToUpdate;
    }catch(DMlException de ){
      System.debug(de );
    }
}

Try this one and select as best answer if it helps you. So it will be easy for others to search easily.
 
Neetu_BansalNeetu_Bansal
Hi,

You mean to say OB__c is a debit object and it needs to be removed multiple time from the BankBook__c object?

Thanks,
Neetu