I have a requirement where I need to call a external WS with the item code whenever a new item is added in BC. Here is my code snippet (i used http GET to test, ideally this will be a POST).
codeunit 50106 "Item No. Webhook"
{
[EventSubscriber(ObjectType::Table, Database::Item, 'OnAfterInsertEvent', '', false, false)]
local procedure NotifyExternalSystemAfterInsert(var Rec: Record Item)
var
HttpClient: HttpClient;
HttpResponseMessage: HttpResponseMessage;
QueryStringUrl: Text;
IsSuccess: Boolean;
begin
// Construct the URL with the item number as a query string
//QueryStringUrl := 'https://3e1a-2405-201-f00d-a843-b92f-e21d-cb98-c890.ngrok-free.app/webhook?itemnum=' + Rec."No.";
QueryStringUrl := 'https://www.google.com/';
// Send HTTP GET request
IsSuccess := HttpClient.Get(QueryStringUrl, HttpResponseMessage);
// Check if the request was successful
if IsSuccess then begin
if HttpResponseMessage.IsSuccessStatusCode() then
Message('Item number "%1" successfully sent to the external system.', Rec."No.")
else
Message('Failed to notify external system for item "%1". Status Code: %2', Rec."No.", HttpResponseMessage.HttpStatusCode());
end else
Message('Error occurred while connecting to the external system for item "%1".', Rec."No.");
end;
}