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

The follwoing is the technique I use to trigger code to run at the end of the deepinsert. Hope it helps
The following Post Body
{
“no”: 1907,
“txtfield1”: “header1”,
“testLines”:[
{“txtfield1”:“Line1”},
{“txtfield1”:“Line2”}
]
}

Theis is the api page trigger sequence when inserting a header with 2 lines in a single post (deep insert)
1 Header OnInit
2 Header OnOpenPage
3 Header OnNewRecord
4 Header OnValidate Field (Could be multiple)
5 Header OnInsertRecord header1
6 Line OnInit
7 Line OnNewRecord
8 Line OnValidate Field (Could be multiple)
9 Line OnInsertRecord Line1
10 Line OnNewRecord
11 Line OnValidate Field (Could be multiple)
12 Line OnInsertRecord Line2
13 Header OnAfterGetRecord
14 Header OnAfterGetCurrRecord

The key here is that the Header OnAfterRecord is run again at the bottom of the sequence and is used to send back the json reply to the post. As a result the secret to running code after the completion of the deep insert is as follows

  1. In Trigger 3 set a page variable deepinsertinprocess to true (this is needed as the onaftergetrecord is also run during a get call and we only want certain behavior in the get call if it as a result of the post insert call)
  2. In Trigger 13 Header OnAfterGetRecord test if deepinsertinprocess is true and then conduct code - for example check if there are more than 2 lines. At this point you can access the sublines as records that match the current Rec. See below. Since we are still in the current transaction scope the error we are raising will rollback the original insert! This can also be used to run code after the completion of the entire post Header and Lines!

trigger OnAfterGetRecord()
var
a: Record testapiLines;
b: Record testapiHeader;
begin
if deepinsertinprocess then begin
a.SetRange(a.HeaderLink, rec.SystemId);
if a.FindSet() then begin
if a.Count <> 2 then
Error(‘Fail On line item count check’);
end;
end;
end;