I have a small function which basically send a request to the webservice and return the response. But if the server is not available then it generates the run time exception. I want to handle this exception. Please tell me how to do this? I have two options in my mind
1. Try Function (as it is Dynamics NAV 2016 version)
2. Use codeunit 1291. DotNet Exception Handling.
If i make this function as TRY function (approach 1) then i am not able to return the response. I want to get the response because if the response is successful, i want to DELETE the row from my table. If the response is not successful then I have to send an email. Everything is working fine but i have to cover that exception so that it does not halt my job.
Please tell me which approach is good and how can i handle it. How to use approach 2 if it is good?
My Webservice code is as under:
The response of the webservice which i am getting and using code is as under:
Thanks in advance.
1. Try Function (as it is Dynamics NAV 2016 version)
2. Use codeunit 1291. DotNet Exception Handling.
If i make this function as TRY function (approach 1) then i am not able to return the response. I want to get the response because if the response is successful, i want to DELETE the row from my table. If the response is not successful then I have to send an email. Everything is working fine but i have to cover that exception so that it does not halt my job.
Please tell me which approach is good and how can i handle it. How to use approach 2 if it is good?
My Webservice code is as under:
CallWebService(BaseURL : Text;Method : Text;RestMethod : Text) : Text HttpClient := HttpClient.HttpClient(); HttpClient.BaseAddress := Uri.Uri(BaseURL); CASE RestMethod OF 'GET': BEGIN HttpResponseMessage := HttpClient.GetAsync(Method).Result; END; 'POST': BEGIN HttpResponseMessage := HttpClient.PostAsync(Method,HttpContent).Result; END; END; EXIT(FORMAT(HttpResponseMessage.Content.ReadAsStringAsync.Result));
The response of the webservice which i am getting and using code is as under:
Result := CallWebService(IntegrationSetup."URL", STRSUBSTNO('api/Books/UpdateSales?bookId=%1&sellingQuantity=%2',"Item No.","Qty. in stock", "Selling Source"),'POST'); IF UPPERCASE(Result) = API_RESPONSE THEN DELETE ELSE IF IntegrationSetup."Support Email" <> '' THEN BEGIN Email.CreateMessage('Sender Name',SMTPSetup."User ID",IntegrationSetup."Support Email",'Error in webservice', Result,FALSE); Email.Send; END;
Thanks in advance.