Skip to main content

Notifications

Announcements

No record found.

Finance | Project Operations, Human Resources, ...
Suggested answer

500 Internal Server Error During PUT Request from D365FO to Third-Party App

(1) ShareShare
ReportReport
Posted on by 2,311
Hi everyone,
 
I am working on an integration that updates main account data from D365FO to a third-party application. The integration works well if the account exists in the third-party application. However, if the record is deleted directly from the third-party application, an error occurs when the application tries to update it.
 
Error: 
System.Net.WebException: The remote server returned an error: (500) Internal Server Error.
 
Expected Behavior: 
When updating the same record from Postman, a more specific and user-friendly error message is displayed:
> Database operation expected to affect 1 row(s) but actually affected 0 row(s). 
> Data may have been modified or deleted since entities were loaded.
 
I would appreciate any help from experts to identify the cause of this issue. Here is the sample code I used if needed.
 
internal final class APIClientCallRunnableClass
{
    public static void main(Args _args)
    {
        APIClientCallRunnableClass::APICallHttpWebRequest();
    }
    public static void APICallHttpWebRequest()
    {
        System.Net.HttpWebRequest    request;
        System.IO.Stream             stream;
        System.Exception             sysEx;
        System.Net.WebException     webEx;

        str apiUrl, apimSubscriptionKey, xClientId, mimeType, responseBody;

        apiUrl              = 'https://3rd-party-application-api-url';
        apimSubscriptionKey = 'my-subscription-key';
        xClientId           = 'my-client-id';
        mimeType            = 'application/json';

        request = System.Net.WebRequest::Create(apiUrl) as System.Net.HttpWebRequest;
        request.Method = 'PUT';
        request.ContentType = 'application/json';

        // Set the request headers
        System.Net.WebHeaderCollection headerCollection = request.Headers;
        headerCollection.Set('Ocp-Apim-Subscription-Key', apimSubscriptionKey);
        headerCollection.Set('X-Client-Id', xClientId);
        headerCollection.Set('name', mimeType);

        var utf8 = System.Text.Encoding::get_UTF8();

        // Set the request body
        var byteArrayPayload = utf8.GetBytes("{\"accountCode\":\"999991\",\"accountName\":\"999991 ADDED/UPDATED AGAIN\",\"entityCode\":\"USMF\",\"entityName\":\"Contoso Entertainment System USA\",\"id\":9739}");
        
        try
        {
            // send out the payload
            using(System.IO.Stream dataStream = request.GetRequestStream())
            {
                dataStream.Write(byteArrayPayload, 0, byteArrayPayload.Length);
            }

            // request.GetResponse() may already result in an error if the request was e.g. a Bad Request(Status Code 400). This should be handled upstream via our global error handling.
            using(System.Net.HttpWebResponse response = request.GetResponse() as System.Net.HttpWebResponse)
            {
                stream = response.GetResponseStream();
                System.IO.StreamReader reader = new System.IO.StreamReader(stream);
                responseBody = reader.ReadToEnd();
            }
        }
        catch (webEx)
        {
            Error(strFmt('An error occured while updating record: %1', webEx.Message));
        }
        catch (sysEx)
        {
            Error(strFmt("An error occured while updating record: %1", sysEx.Message));
        }
    }
}
 
Categories:
  • Martin Dráb Profile Picture
    Martin Dráb 230,540 Most Valuable Professional on at
    500 Internal Server Error During PUT Request from D365FO to Third-Party App
    Are you saying that you get a response object with detailed messages when calling the web service from Postman and want to add code to apiCallHttpWebRequest() to expose this data?
     
    Your description also suggest that you get an unhandled exception, which sounds strange (because you're catching System.Exception). Please tell us more about the observed behaviour.
  • Suggested answer
    Mohamed Amine Mahmoudi Profile Picture
    Mohamed Amine Mahmoudi 11,290 Super User 2024 Season 2 on at
    500 Internal Server Error During PUT Request from D365FO to Third-Party App
     
    It's normal to get this error message since the record doesn't exist in the third-party application.
    I suggest you do a GET before the PUT if the record exists you must do PUT if not POST.
     
    Best regards,
    Mohamed Amine MHMOUDI

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

Congratulations 2024 Spotlight Honorees

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December

Congratulations to our December super stars! 🥳

Start Your Super User Journey Pt 2

Join the ranks of our community heros! 🦹

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,861 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,540 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans