API Pages AfterInsert Logic in Triggers OnQueryClosePage/OnClosePage
Hi,
I need to add some AfterInsert functionality to a Custom API Page where the record of the API is already inserted. To be more specific, if a Customer is inserted via this API Page a new Contact Business Relation shall be created if a linked Contact has been found. After some testing I have discoverd that the OnQueryClosePage/OnClosePage is run when the Customer is already inserted and that this newly created Customer can be Validated into the "No." field of the Contact Business Relation. My question is: Is the OnCloseQueryPage Trigger on API Pages intended to behave like that and can be used to handle e.g. AfterInsert logic?
Kind Regards
@nikolakukrika - can you help?
Hi, OnQueryClosePage and OnClosePage triggers are not intended to handle web service clients. They are designed to be invoked only when page is hosted in a modal popup window (MPO), therefore a UI client. Instead you can use the OnInsertRecord trigger and insert the record yourself, that way you'd be sure that the record is inserted and have your additional logic afterwards. Another option could be to subscribe to OnAfterInsertRecord event on the source table, and check the client type.
[EventSubscriber(ObjectType::Table, Database::Customer, 'OnAfterInsertEvent', '', false, false)]
local procedure HandleOnAfterCustomerInsert(var Rec: Record Customer)
var
ClientTypeManagement: Codeunit "Client Type Management";
begin
if ClientTypeManagement.GetCurrentClientType() = ClientType::ODataV4 then
//do stuff
end;
Thank you @onbuyuka, issue can be closed