Deep Insert last line insert triggering or whole batch insert triggering

Hi,
I’m creating API pages to use deep insert. And fond out laсk of information about this topic. For example:

  1. how to trigger the last line inserting in the request ? (I’m going to run a codeunit after that). Without adding extra marker-fields. Trigger “OnClosePage” doesn’t work.
  2. how to trigger/handle response text and status? Is it possible?
  3. Are there some documentation with step by step explanation how it works and what we can handle/trigger from BC?

Hi @Dmytro_Yudin,

1- No, you cannot trigger a process with the last line inserted because you doesn’t know, in the API page, witch line is currently being inserted. Try to use an API bound action to run a process on the main record created, this requires another API call after the deep insert. As an alternative, you can create a Job Queue Entry when main record is inserted to trigger the process you need a few seconds later.

2- The API page always response to an INSERT (POST) with all API data included in both, main record and its lines. Check out this: Deep insert with Business Central APIs – Kauffmann @ Dynamics 365 Business Central. I’m not sure what you want to handle/trigger, but you can add the fields you need to the API page, and return the values you need to handle the response the way you need.

3- There are really few documentation about deep insert in Business Central, the previous link from MVP Kauffmann is one (and the 2nd part: Deep insert with Business Central APIs (part 2) – Kauffmann @ Dynamics 365 Business Central), the official documentation from Microsoft only offers one little example (Developing a custom API - Business Central | Microsoft Learn), almost useless.

Hi @pjllaneras ,
Thank you very much for answers.
1- it’s a pity …
2- can I edit response status number : 200, 400 … ? For example, even request is successful but I want to return 400 and don’t use ERROR for this reason to avoid roll back?
3- it’s a pity … :frowning:

No, you cannot control the response status… ok, you only control if API call has been successfull (200) or error (400), but if you trigger an error, a rollback is done. You can try to control this yourself and trigger an error or not, but doing a COMMIT before an error to not rollback modifications, but this only can be done if you use the bound action, not in the INSERT/MODIFY
(POST/PATCH) operations in API page. Something like this:

page 50000 MyApiPage
{
    ...
    [ServiceEnabled]
    procedure runTask(var actionContext: WebServiceActionContext)
    begin
        if Codeunit.Run(Codeunit:MyProcess, Rec) then begin
            DoSomething(Rec);
            Commit;
            error('Process is fine, but I want to return status 400');
        end
        else begin
            message(GetLastErrorText); // this will return status '200', but nothing will be saved because an error has been triggered in the codeunit
            actionContext.SetResultCode(WebServiceActionResultCode::None);
        end;
    end;
}
1 Like