Skip to main content

Notifications

Business Central forum
Unanswered

Perform action after API request

Posted on by 5

Hello, 

Is it possible to call functions after API request?

My case: 

I prepared API page for Business Central and now I need to call function after the call is send. I tired to use triggers: OnClosePage, OnQueryClosePage but those two are not working. Is there any other possibility to call function after API request in BC?

Categories:
  • JV-15051434-0 Profile Picture
    JV-15051434-0 4 on at
    Perform action after API request
    Hi Guys,
     
    I think that the post action is a really weird one. Sure it is cool that you can call it, but when working with other interfaces this is really not practical. There should be an action like OnAfterApiPayload or something like that. Which in my opinion could be done in the C# source code of AL.
     
    How i solved it in a real live situation is to add a field for the last line. So if you post an sales order, the last sales line has the field "LastLine" boolean true. You can then start a session from the onafterinsertSalesLine (or something like that). The sending side has to put that in the json message. The benefit is that it is in one call, so there is no risk of first sending the data and then failing the action post. 
     
    Another idea is to start a session that checks if the order is in after a certain amount of time. Lame, i know, but better then some sort of job queue that can also fail after an update or something.
     
    Bottomline, if there is an idea to make a OnAfterApiPayload event i would vote for it. Heck i'll even campagne for it to make it real. That would save a lot of headaches. 
  • TriposDev Profile Picture
    TriposDev 10 on at
    RE: Perform action after API request

    Hi, I have a different scenario:

    When I call my API, I have to skip all onValidate triggers in the table because I always get all the errors related to empty fields even if they are all filled.

    So I created an event in the table that uses the standard "isHandled" method, but I'm using the User Setup table to set a custom boolean field (isApi).

    So, when the OnOpenPage trigger (in the page API) is executed, I set the isApi field in User Setup to true.

    So far, it works. My problem is setting it back to false when the call is ended.

    I tried OnClosePage and OnQueryClosePage too, but the field is always true now; even if the page is closed.

  • YUN ZHU Profile Picture
    YUN ZHU 69,325 Super User 2024 Season 2 on at
    RE: Perform action after API request

    Hi, just adding a simple example.

    How to run AL procedure in Power Automate (Add custom execute actions)

    https://yzhums.com/20111/

    Hope this helps as well.

    Thanks.

    ZHU

  • DomRot Profile Picture
    DomRot 15 on at
    RE: Perform action after API request

    Thanks you very much Inge.

  • Inge M. Bruvik Profile Picture
    Inge M. Bruvik 32,744 Moderator on at
    RE: Perform action after API request

    If you want to perform a posting as part of an API call you should use the bound actions in the API.

    You can look here for some examples.

    docs.microsoft.com/.../devenv-creating-and-interacting-with-odatav4-bound-action

    robertostefanettinavblog.com/.../

  • DomRot Profile Picture
    DomRot 15 on at
    RE: Perform action after API request

    Hi Inge, thanks for your suggestion. I've tested this posting some inventory adjustment and worked fine.

    My api will receipt some other kind of docs like purchase receipts and sales invoice that I will try to posting in the next days.

    Why you do not recommend this trigger?

    Thanks again.

  • Inge M. Bruvik Profile Picture
    Inge M. Bruvik 32,744 Moderator on at
    RE: Perform action after API request

    I would not recommend you to use that trigger for posting.

    You should look at some other event or some other way to do your posting.

    What is your buisness scenario ?

  • DomRot Profile Picture
    DomRot 15 on at
    RE: Perform action after API request

    Hi, I search your post because I have same problem. After some search, I have seen that the event that fire after post request, is the OnBeforeCompanyClose.

    Subscribing this event in my cu, I can perform bc post documents.

  • DevPl320 Profile Picture
    DevPl320 5 on at
    RE: Perform action after API request

    Thanks for your time, job solution looks like the best option.

  • Inge M. Bruvik Profile Picture
    Inge M. Bruvik 32,744 Moderator on at
    RE: Perform action after API request

    It is possible but i would not do it that way.

    What i would do is to create a time trigger job in the job queue that every x minutes looks for new records in doc header and then post them. This is because than a failure in your posting will not affect the whole flow of your API.

    So separate the insert transaction through the API from the posting process. Allowing potential errors to be handle in what my opinion is a better way.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Dynamics 365 Community Update – Sep 16th

Welcome to the next edition of the Community Platform Update. This is a weekly…

Announcing Our 2024 Season 2 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Dynamics 365 Community Newsletter - September 2024

Check out the latest community news

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,480 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 228,388 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans