Good morning,
I am working on a function in Epicor, and after saving and promoting it I got this error. The function makes GET requests to a third-party API. See code below error. Do I have to worry about this error?
GetDeptHrs.cs(122,13): warning CS0618: ‘RestClient.ExecuteAsync(IRestRequest, Action<IRestResponse, RestRequestAsyncHandle>)’ is obsolete: ‘This method will be removed soon in favour of the proper async call’
Function GetDeptHrs(deptKey in, myDate in, deptHrs out)
try
{
// Base URL for the API
var URL = "URLSTRING";
// Initialize RestClient
var client = new RestClient(URL);
// Parse myDate to match the expected format for the API call
if (!DateTime.TryParseExact(myDate.ToString(), "M/d/yyyy h:mm:ss tt", null, System.Globalization.DateTimeStyles.None, out DateTime dateTimeValue))
{
// Debug message for invalid date format
myDebugMsg += "[ERROR] Invalid date format";
return; // Exit the try block as date parsing failed
}
string formattedDate = dateTimeValue.ToString("yyyy-MM-dd");
// Debug message for formatted date
myDebugMsg += "Formatted Date: " + formattedDate + " | ";
// Construct the API request URL
var myRequest = "webapi/timecards/level/" + deptKey + "/payrolldata/" + formattedDate;
myDebugMsg += myRequest;
// Create a RestRequest with the constructed URL and GET method
var request = new RestRequest(myRequest, Method.GET);
// Add authorization header
request.AddHeader("Authorization", "Basic AUTHSTRING");
// Execute the request
IRestResponse myIResponse = client.Execute(request);
// Check if request was successful
if (myIResponse.StatusCode != System.Net.HttpStatusCode.OK)
{
// Debug message for failed API request
myDebugMsg += "[ERROR] API request failed: " + myIResponse.ErrorMessage + " | ";
return; // Exit the try block as API request failed
}
// Extract response content
string responseData = myIResponse.Content;
// Debug message for successful API response
myDebugMsg += "API Response: " + responseData + " | ";
// Parse the response data into a JSON array
JArray jsonData;
try
{
jsonData = JArray.Parse(responseData);
}
catch (Newtonsoft.Json.JsonReaderException ex)
{
// Debug message for JSON parsing error
myDebugMsg += "[ERROR] JSON parsing error: " + ex.Message + " | ";
return; // Exit the try block as JSON parsing failed
}
// Calculate the total hours for selected PayTypeIDs
int totalMinutes = 0;
List<string> excludedPayTypeIDs = new List<string> { "JURY", "HOL", "BER", "SICK/VACA", "S1A HOL", "S2A HOL", "S1B HOL", "S2B HOL" };
int requestsCompleted = 0;
int requestsTotal = 0;
foreach (JObject record in jsonData)
{
// Extract report date and hours
string reportDate = DateTime.Parse(((string)record["ReportDate"]).Split('T')[0]).ToString("yyyy-MM-dd");
if (reportDate == formattedDate && !excludedPayTypeIDs.Contains(record["PayTypeId"].ToString()))
{
requestsTotal++;
// Sending employee status request
var employeeKey = record["EmployeeKey"].ToString();
var employeeRequest = new RestRequest("webapi/2/employees/" + employeeKey, Method.GET);
employeeRequest.AddHeader("Authorization", "Basic AUTHSTRING");
client.ExecuteAsync(employeeRequest, (response, handle) =>
{
requestsCompleted++;
if (response.ErrorException != null)
{
Console.WriteLine(response.ErrorException);
return;
}
var employeeData = JObject.Parse(response.Content);
if (employeeData["EmployeeStatus"]["Description"].ToString() == "Active")
{
totalMinutes += Convert.ToInt32(record["Minutes"]);
}
if (requestsCompleted == requestsTotal)
{
deptHrs = ((decimal)totalMinutes / 60);
}
});
}
}
}
catch (Exception ex)
{
// Debug message for other exceptions
myDebugMsg += "[ERROR] Exception: " + ex.Message + " | ";
}
The basic idea of this function is to pull the total hours from the department. It only gets hours for the employee if they are “active” and it only takes working hours, not vacation, sick, holiday, etc.
When I convert the Async request to a regular request to get rid of that warning, the function seems to be returning 0 or more likely failing in the background. I can’t tell. This is the code I used when I converted from Async.
try
{
// Base URL for the API
var URL = myURL;
// Initialize RestClient
var client = new RestClient(URL);
// Parse myDate to match the expected format for the API call
if (!DateTime.TryParseExact(myDate.ToString(), "M/d/yyyy h:mm:ss tt", null, System.Globalization.DateTimeStyles.None, out DateTime dateTimeValue))
{
// Debug message for invalid date format
myDebugMsg += "[ERROR] Invalid date format";
return; // Exit the try block as date parsing failed
}
string formattedDate = dateTimeValue.ToString("yyyy-MM-dd");
// Debug message for formatted date
myDebugMsg += "Formatted Date: " + formattedDate + " | ";
// Construct the API request URL
var myRequest = "webapi/timecards/level/" + deptKey + "/payrolldata/" + formattedDate;
myDebugMsg += myRequest;
// Create a RestRequest with the constructed URL and GET method
var request = new RestRequest(myRequest, Method.GET);
// Add authorization header
request.AddHeader("Authorization", "Basic");
// Execute the request
IRestResponse myIResponse = client.Execute(request);
// Check if request was successful
if (myIResponse.StatusCode != System.Net.HttpStatusCode.OK)
{
// Debug message for failed API request
myDebugMsg += "[ERROR] API request failed: " + myIResponse.ErrorMessage + " | ";
return; // Exit the try block as API request failed
}
// Extract response content
string responseData = myIResponse.Content;
// Debug message for successful API response
myDebugMsg += "API Response: " + responseData + " | ";
// Parse the response data into a JSON array
JArray jsonData;
try
{
jsonData = JArray.Parse(responseData);
}
catch (Newtonsoft.Json.JsonReaderException ex)
{
// Debug message for JSON parsing error
myDebugMsg += "[ERROR] JSON parsing error: " + ex.Message + " | ";
return; // Exit the try block as JSON parsing failed
}
// Calculate the total hours for selected PayTypeIDs
int totalMinutes = 0;
List<string> excludedPayTypeIDs = new List<string> { "JURY", "HOL", "BER", "SICK/VACA", "S1A HOL", "S2A HOL", "S1B HOL", "S2B HOL" };
int requestsTotal = 0;
foreach (JObject record in jsonData)
{
// Extract report date and hours
string reportDate = DateTime.Parse(((string)record["ReportDate"]).Split('T')[0]).ToString("yyyy-MM-dd");
if (reportDate == formattedDate && !excludedPayTypeIDs.Contains(record["PayTypeId"].ToString()))
{
requestsTotal++;
// Sending employee status request
var employeeKey = record["EmployeeKey"].ToString();
var employeeRequest = new RestRequest("webapi/2/employees/" + employeeKey, Method.GET);
employeeRequest.AddHeader("Authorization", "Basic");
// Execute the request synchronously
IRestResponse response = client.Execute(employeeRequest);
if (response.ErrorException != null)
{
// Handle error properly
myDebugMsg += "[ERROR] Exception during employee request: " + response.ErrorMessage + " | ";
continue;
}
// Check if request was successful
if (response.StatusCode != System.Net.HttpStatusCode.OK)
{
// Debug message for failed API request
myDebugMsg += "[ERROR] API request failed: " + response.ErrorMessage + " | ";
return; // Exit the try block as API request failed
}
// Parse the response data into a JSON array
JArray employeeDataArray;
try
{
employeeDataArray = JArray.Parse(response.Content);
}
catch (Newtonsoft.Json.JsonReaderException ex)
{
// Debug message for JSON parsing error
myDebugMsg += "[ERROR] JSON parsing error: " + ex.Message + " | ";
return; // Exit the try block as JSON parsing failed
}
if (employeeDataArray["EmployeeStatus"]["Description"].ToString() == "Active")
{
totalMinutes += Convert.ToInt32(record["Minutes"]);
}
}
}
// Avoid division by zero
if (requestsTotal != 0)
{
deptHrs = ((decimal)totalMinutes / 60);
}
else
{
myDebugMsg += "[ERROR] Division by zero";
}
}
catch (Exception ex)
{
// Debug message for other exceptions
myDebugMsg += "[ERROR] Exception: " + ex.Message + " | ";
}
I appreciate any advice you all have to offer. Thanks for your time!
Nate