Page 1 of 1

How to add BusinessLogic event on custom BusinessLogic

PostPosted: Wed May 10, 2023 4:58 pm
by sameermoin
Hi,

I have one custom plugin in which I have one custom form inheriting JiwaFinancials.Jiwa.JiwaApplication.BusinessLogic.Configuration class. Like in other forms such as the Inventory maintenance form, we have SaveEnd, SaveStart, and other events I want to implement one of them in the custom form. The custom business logic class has been implemented as a .NET library and embedded into the plugin as a dll 

Although I am getting the events, none of them are working.

Code: Select all
public partial class WebMaintenance : JiwaFinancials.Jiwa.JiwaApplication.BusinessLogic.Configuration
{
   //custom logic
}


And this is how I am trying to use it:

Code: Select all
if (JiwaBusinessLogic is CustomBusinessLogic.BusinessLogic.WebMaintenance)
{
      CustomBusinessLogic.BusinessLogic.WebMaintenance webMaintenance = (CustomBusinessLogic.BusinessLogic.WebMaintenance)JiwaBusinessLogic;

      webMaintenance.SaveEnd += delegate (object sender, EventArgs e)
      {

      };
}

Re: How to add BusinessLogic event on custom BusinessLogic

PostPosted: Wed May 10, 2023 8:57 pm
by Mike.Sheen
sameermoin wrote:Although I am getting the events, none of them are working.


What does that mean? If you are getting the event, then it is working. What do you mean by it is not working?

You have not provided enough information to help you. Perhaps if you provided a sample plugin illustrating the problem, we could help you more.

Re: How to add BusinessLogic event on custom BusinessLogic

PostPosted: Fri May 12, 2023 3:49 pm
by Nina@Opal
Hi Mike,

We have created a .NET class library to implement custom business logic and referenced it in a plugin as an embedded reference.

The business logic class inherit from Jiwa configuration business logic base class JiwaFinancials.Jiwa.JiwaApplication.BusinessLogic.Configuration.

Then we wanted to hook into the business logic object SaveEnd event from another plugin. We added a plugin reference to the second plugin we are developing and we noticed that the code intellisense in the code editor works as expected, but the subscribed SaveEnd event does not trigger as expected.

Do we need to do anything else other than referencing first plugin we did in order to get this working?

Thanks
Nina

Re: How to add BusinessLogic event on custom BusinessLogic

PostPosted: Fri May 12, 2023 5:42 pm
by SBarnes
Hi Nina,

Below is the code for the object you are inheriting from in terms of its save so I will assume you have implemented iSave with no code to reference to get at the specifics like Mike asked on this post previously this is a guess at best


Code: Select all
 Protected MustOverride Sub iSave() Implements IJiwaSaveable.iSave
        Public Sub Save() Implements IJiwaSaveable.Save
            Try
                iSave()
            Catch ex As JiwaODBC.Exceptions.SQLTransactionIsZombied
                Dim retryCount As Integer = 0
                Dim retryInterval As Integer

                If Manager.Database.RetriesOnTransientFailure > 0 Then
                    Do While retryCount <= Manager.Database.RetriesOnTransientFailure
                        Try
                            If retryCount > 0 Then
                                Thread.Sleep(retryInterval) 'Exponential back-off
                            End If
                            iSave()
                            Exit Do
                        Catch ex2 As Exception
                            iSave()
                            Exit Do
                        End Try
                        retryInterval += Manager.Database.RetryBaseMillisecondsInterval * retryCount
                        retryCount += 1
                    Loop
                    Throw
                Else
                    Throw
                End If
            Catch ex3 As Exception
                Throw
            End Try
        End Sub


There are also three methods that will raise the save events namely those shown below these should go in the appropriate places within your iSave code to call any attached events to trigger

Code: Select all
Public Overridable Sub OnSaveStart()
            RaiseEvent SaveStart(Me, New System.EventArgs)
        End Sub

        Public Overridable Sub OnSaveEnding()
            RaiseEvent SaveEnding(Me, New System.EventArgs)
        End Sub

        Public Overridable Sub OnSaveEnd()
            RaiseEvent SaveEnd(Me, New System.EventArgs)
        End Sub



An example of this is the iSave from General Ledger configuration that looks like this and is a class that inherits from the class you mentioned, note that the difference between ending and end is ending should be called before any database commits and end should be called as the last thing.

Code: Select all
 Protected Overrides Sub iSave()
        Dim weStartedTransaction As Boolean

        With Manager.Database
            Try
                If .SQLTransaction Is Nothing Then
                    ' flag that we created the transaction, so we know if we should be the ones to .commit or .rollback later.
                    .BeginNewTransaction()
                    weStartedTransaction = True
                End If

                OnSaveStart()

                ' Validate no years or periods overlap and ensure Year staring dates match Period 1 starting date
                Years.Validate()

                Years.Save()
                Manager.Database.SaveSysData("GLParams", "SegmentSeparator", _Separator)
                Segments.Save()

                OnSaveEnding()

                If weStartedTransaction Then
                    .SQLTransaction.Commit()
                    .SQLTransaction.Dispose()
                    .SQLTransaction = Nothing
                End If

                Manager.GeneralLedgerConfiguration.Read()

                OnSaveEnd()

            Catch ex As Exception
                If weStartedTransaction AndAlso .SQLTransaction IsNot Nothing Then
                    If .SQLTransaction.Connection IsNot Nothing Then
                        .SQLTransaction.Rollback()
                    End If
                    .SQLTransaction.Dispose()
                    .SQLTransaction = Nothing
                End If

                Throw
            Finally
                If weStartedTransaction Then
                    .SQLTransaction = Nothing
                End If
            End Try
        End With

    End Sub

Re: How to add BusinessLogic event on custom BusinessLogic

PostPosted: Fri May 12, 2023 5:46 pm
by Mike.Sheen
Nina@Opal wrote:Do we need to do anything else other than referencing first plugin we did in order to get this working?


Without seeing your code, I can't be sure, but it's probably because in your Save method you are not calling the OnSaveEnd() method.

Re: How to add BusinessLogic event on custom BusinessLogic

PostPosted: Mon May 15, 2023 8:02 pm
by sameermoin
Check the attached example plugin with a custom "Lib.LibBusinessLogic" business logic. The embedded library code is also linked in the plugin document section.

Plugin Embedded Lib Plugin.xml
(62.52 KiB) Downloaded 70 times

Re: How to add BusinessLogic event on custom BusinessLogic

PostPosted: Tue May 16, 2023 8:37 am
by SBarnes
Ok having had a quick look at things the embedded code looks ok but what I suspect you problem is, as I haven't tested this, is that under the business logic tab you do not have your business logic object listed so Jiwa doesn't look for your business object being created and setup to call the business logic setup call in the plugin.

You need an entry in the SY_BusinessLogic table to make this happen and then add it under the business logic tab in the plugin. The SQL for the search screen on that tab is below, have a look at entries in table to see what to do.

The other thing is your embedded solution is using .net framework 4.7.2 you will need to install this on any machine running Jiwa, switch it back to 4.7.1 if you can to make your life easier.


Code: Select all
SELECT RecID, Description, AssemblyFullName, ClassName FROM SY_BusinessLogic
ORDER BY Description


This why providing a plugin and not just snippets of code is always a quicker way to reach an answer as in this case the problem may not be the actual code itself but rather the plugin setup.

Re: How to add BusinessLogic event on custom BusinessLogic

PostPosted: Tue May 16, 2023 11:00 am
by Mike.Sheen
This is precisely as Stuart described.

Attached is a modified version of the plugin which works.

It has a SQL Script attached to the documents tab, which when run will register the business logic.

Once run, you could then add the business logic to the business logic references tab of the plugin.

So, you need to import the attached plugin, save, then run the attached SQL script, then exit, log back in and it will work as expected.

Re: How to add BusinessLogic event on custom BusinessLogic

PostPosted: Wed May 17, 2023 3:23 pm
by sameermoin
I installed the attached plugin but I am getting an object reference null error when logging in Jiwa.

jiwa error.png
jiwa error.png (21.22 KiB) Viewed 2637 times


It will work fine if I put the Lib.dll in the jiwa Installation directory. In production, we can't put the custom dll in the Jiwa directory.

Re: How to add BusinessLogic event on custom BusinessLogic  Topic is solved

PostPosted: Wed May 17, 2023 4:45 pm
by Mike.Sheen
sameermoin wrote:I installed the attached plugin but I am getting an object reference null error when logging in Jiwa.

jiwa error.png


It will work fine if I put the Lib.dll in the jiwa Installation directory. In production, we can't put the custom dll in the Jiwa directory.


Odd - I get it now, too - but not when I first enabled the plugin and logged out and logged in.

What you're trying to do with this plugin isn't something we'd anticipated - you have an embedded dll reference you want registered as a business logic object, and to reference that same object in the same plugin - and that's not a normal pattern.

I've logged DEV-9989 to look at this, but in the meantime, don't embed your business logic as an embedded reference, but just inside the plugin itself. If you want other plugins to reference it, they can by adding a plugin reference.

What you are trying to do is hide whatever is in your business logic - and that's not something we encourage as it can mean the customer is left in a potentially unsupportable situation in the future. Whatever code you have in there isn't as special or important as you may think.