Support Topics Documentation Slack YouTube Blog

Business logic

(Mohamad Mohamad Jamil) #1

So here what I want to achieve: I want to create an event that runs after I create a new object in the database. And this function has to dispatch a custom event. So I use the backendless afterCreate method: Backendless.ServerCode.Persistence.afterCreate(); And Backendless.Events.dispatch() non-blocking method to dispatch a custom event.
When I save a new object by using the REST API, then the afterCreate() works fine but it does not dispatch the custom event! Another issue is that if I save a new object to the database by using API Services, the afterCreate() method does not run!?

This is the afterCreate() code that I use :

And this is the method of a service I use to save a new object :

I do not know If I use it in the correct way!

(Oleg Vyalyh) #2

Hi, Jamil.
We have a restrictions on calling business logic from the business logic. So you cannot invoke custom service method from another custom service. Such behaviour touches event handlers too.

(Mohamad Mohamad Jamil) #3

So do you mean that I can not invoke a custom event handler from a custom service method?
And why the afterCreate() does not run when I save an object by using a custom service method, as I had done in the code above?

(Oleg Vyalyh) #4
  1. No, you can not.
  2. As i said before, the same rule relates with handlers too. If you perform saving object in the table which has xxxCreate handler, the invocation of another saving method won’t force business logic invocation. It is error prone approach. Increasing and complication logic of application would cause looping the business logic invocation in some cases.