Page 1 of 1

Cashbook receipt Jiwa crash when desc blank

PostPosted: Thu Apr 14, 2022 10:05 pm
by indikad
Jiwa 7.2.1
Window: Cash book Payment
Issue replication: Create a new Payment , go straight to type a value on the new transaction line and tab out - Jiwa crashes
If you have some text in the Header Description field then all is well

Code: Select all
public void Setup(JiwaFinancials.Jiwa.JiwaApplication.IJiwaForm JiwaForm, JiwaFinancials.Jiwa.JiwaApplication.Plugin.Plugin Plugin)
    {
      if(JiwaForm is JiwaFinancials.Jiwa.JiwaCashBookUI.frmCashBook)
      {
         JiwaFinancials.Jiwa.JiwaCashBookUI.frmCashBook cashBookForm = (JiwaFinancials.Jiwa.JiwaCashBookUI.frmCashBook)JiwaForm;         
         _cashBookForm =  cashBookForm;
         _Plugin = Plugin;         
         cashBookForm.CashBook.CashBookTransactions.Added += CashBook_TransactionAdded;            
         cashBookForm.CashBook.CashBookTransactions.Changed  += CashBookTransactions_Changed;
      }      
    }
   private void CashBook_TransactionAdded(JiwaFinancials.Jiwa.JiwaCashBook.CashBookTransaction item)
   {   
      try
      {
         if (_cashBookForm.CashBook.Description.ToString().Length > 0 )
         {
            item.Remark =  _cashBookForm.CashBook.Description.ToString() ;   
         }            
         item.PaymentType.ReadRecord ("000000001Z00000000RH"); // demodata setting to AMX                  
      }
      catch(Exception ex)
      {
         MessageBox.Show("Error setting transaction properties in trx add err: " + ex.Message , "Plugin: " + _Plugin.Name  , MessageBoxButtons.OK , MessageBoxIcon.Error  );
      }      
   }
   private void CashBookTransactions_Changed(JiwaFinancials.Jiwa.JiwaCashBook.CashBookTransaction item , System.ComponentModel.PropertyChangedEventArgs e )
   {         
      try
      {         
         if (e.PropertyName.Trim().ToUpper() != "JOBCHARGEAMOUNT" )
         {               
            item.JobChargeAmount = item.HomeAmount * ( (decimal) 1.1 ) ;
         }
         if ( item.Remark.Trim().Length == 0 )
         {
            item.Remark =  _cashBookForm.CashBook.Description.ToString();
         }            
      }
      catch(Exception ex)
      {
         MessageBox.Show("Error setting transaction properties in trx change err: " + ex.Message , "Plugin: " + _Plugin.Name  , MessageBoxButtons.OK , MessageBoxIcon.Error  );
      }   
   }

Re: Cashbook receipt Jiwa crash when desc blank

PostPosted: Fri Apr 15, 2022 9:07 am
by SBarnes
Hi Indika

I tried to replicate your issue in a demo database under 7.2.1 and can't make it fall over, but this copy of Jiwa has the latest service release installed, are you working with the latest service release and are there any other plugins that could be having a side effect on things?

I would try it on a clean demo database with the latest service release installed and see if the issue is still there.

Re: Cashbook receipt Jiwa crash when desc blank

PostPosted: Tue Apr 19, 2022 12:51 pm
by indikad
its the CashBookTransactions.Changed event. looks like it gets hit repeatedly until stack overflows.

-- and there are no other plugins interfering
-- also separated the two event calls to two plugins as a test that has not helped.

-- could be the SR , will have a look

Re: Cashbook receipt Jiwa crash when desc blank

PostPosted: Tue Apr 19, 2022 2:34 pm
by SBarnes
Does it happen if the code you have included is disabled ?

Re: Cashbook receipt Jiwa crash when desc blank

PostPosted: Tue Apr 19, 2022 4:06 pm
by indikad
if I disable the changed event it does not happen.

Re: Cashbook receipt Jiwa crash when desc blank  Topic is solved

PostPosted: Tue Apr 19, 2022 4:20 pm
by SBarnes
Ah then the answer is simple the reason it crashes is because your code in the change event is causing the change event to fire again.

This is similar to recursion with no base case in that the function keeps calling itself endlessly.

What you need to do is kind of similar to how you attached it in that you detach the event, but with a minus instead of the plus which you do at the top of your code, do your code and then reattach the handler at the bottom of your code same as is done in the setup.

Re: Cashbook receipt Jiwa crash when desc blank

PostPosted: Wed Apr 20, 2022 10:33 am
by indikad
Applying the SR fixed it.

Re: Cashbook receipt Jiwa crash when desc blank

PostPosted: Wed Apr 20, 2022 1:50 pm
by indikad
I thought it had fixed with the SR11 but in fact it hadn’t.

I had to place your workaround of detaching the event to get it working. Thanks for that.
it's interesting that it wont "self heal" . I was sure I had written similar plugin code before without having the recursion issue. (may be I am wrong.)

Re: Cashbook receipt Jiwa crash when desc blank

PostPosted: Wed Apr 20, 2022 2:22 pm
by SBarnes
I will be honest when you said the service release had fixed it I was surprised.

As the problem you were having is not uncommon in that the event handler causes the event to fire, and is a common issue to events not just JIWA, the other way you can deal with the issue is to use some sort of Boolean flag and return at the top of the handler if the flag is set but the detach handler option is cleaner and less code.

Re: Cashbook receipt Jiwa crash when desc blank

PostPosted: Wed Apr 20, 2022 5:51 pm
by indikad
agree about the recursion effect is a common issue.

I was actually talking in Jiwa context. I do have some other similar codlings on Jiwa that did not require this intervention. Will have a look and come back when I get some time to spend.