web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Small and medium business | Business Central, N...
Suggested Answer

Calling an external webservice when a new is item added fails

(4) ShareShare
ReportReport
Posted on by 37
Hellio 
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;
}
When I deploy this code and try a new item it errors out. Following line execution is never successful and it always gives the last else message.

IsSuccess := HttpClient.Get(QueryStringUrl, HttpResponseMessage);
Any help in this regard is appreciated. 
Thanks
Prem
 
I have the same question (0)
  • Suggested answer
    Holly Huffman Profile Picture
    6,534 Super User 2025 Season 2 on at
    Hi there! Good morning, evening, or afternoon - depending on where you are :) Hope you are well today! 
     
    The issue you're encountering suggests that the HTTP client in your code is unable to successfully make a call to the external web service. This could be due to several reasons, such as network restrictions, authentication requirements, or incorrect setup. Here are steps to troubleshoot and resolve the issue:
    1. Verify the External Web Service URL
    • Ensure that the URL you’re trying to call (e.g., https://www.google.com/) is correct and reachable from your Business Central environment.
    • Test the URL directly in a browser or a tool like Postman to confirm it responds as expected.
    2. Network or Proxy Issues
    • If the Business Central environment is hosted on-premises, verify network connectivity and ensure that there are no proxy/firewall restrictions blocking outbound HTTP requests.
    • If you’re using a SaaS (cloud-based) environment, ensure that the external URL is accessible from the cloud environment and isn’t blocked.
    3. Missing Service Endpoint Registration
    • Confirm that the external service endpoint has been registered in Business Central. You might need to use an HTTP Client Request for services that require additional configurations, such as headers or authentication tokens.
    4. Enable Web Service Access in Business Central
    • Check the Web Services Setup in Business Central to ensure that external services are properly configured and accessible.
    5. Error Logging
    • Add proper error logging to understand why HttpClient.Get is failing:
      IsSuccess := HttpClient.Get(QueryStringUrl, HttpResponseMessage);
      if not IsSuccess then
          Error('HTTP GET failed. Response: %1', HttpResponseMessage.Content.ReadAs());
    6. Check Authentication
    • If the web service requires authentication (e.g., API keys, OAuth tokens), ensure that your request includes the proper headers:
      HttpClient.DefaultRequestHeaders.Add('Authorization', 'Bearer YOUR_API_TOKEN');
    7. Handle SSL/TLS Issues
    • For URLs using HTTPS, ensure that the SSL/TLS certificate of the external service is valid and trusted by the environment. If the certificate is self-signed or invalid, the request may fail.
    8. Try with a POST Request (as Needed)
    • If the final requirement is to use an HTTP POST, update the code to match the expected request format. For example:
      var
          HttpContent: HttpContent;
      begin
          HttpContent.WriteFrom('{"itemnum":"' + Rec."No." + '"}');
          IsSuccess := HttpClient.Post(QueryStringUrl, HttpContent, HttpResponseMessage);
      End;
    9. Run in Sandbox/Test Environment
    • If possible, test the same code and setup in a Sandbox environment or another environment to rule out configuration-specific issues.
    10. System Application Version Compatibility
    • Verify that the HttpClient functionality works as expected in your Business Central version. If you’re running an older version of Business Central, consider updating to the latest version.
    If these steps don’t resolve the issue, it might help to capture detailed error logs using the Session Debugging Tool in Business Central and analyze them for additional insights.
     
    Hope this helps some!
  • Suggested answer
    Jainam M. Kothari Profile Picture
    15,639 Super User 2025 Season 2 on at
    Hello,
     
    The HTTP client in your code is failing to call an external web service due to potential issues like network restrictions, authentication requirements, or incorrect setup. To resolve this, verify the web service URL, check for network or proxy issues, ensure the service endpoint is registered, enable web service access in Business Central, add error logging, check authentication headers, handle SSL/TLS issues, try using a POST request if needed, test in a sandbox environment, and ensure compatibility with your Business Central version. If the issue persists, detailed error logs can provide further insights.
  • Suggested answer
    Prem A Nair Profile Picture
    37 on at
    thanks all for the response.
     
    i had to turn on "Allow HttpClient Requests" thru the Extension Management for my extension like given below
     
     
     
     

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Small and medium business | Business Central, NAV, RMS

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 2,362

#2
YUN ZHU Profile Picture

YUN ZHU 867 Super User 2025 Season 2

#3
Sumit Singh Profile Picture

Sumit Singh 607

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans