Solved

Unable to cast object of type 'System.Threading.Tasks.Task`1[System.Collections.IEnumerable]' to type 'System.Collections.IEnumerable'

  • 19 May 2023
  • 6 replies
  • 1412 views

Userlevel 3
Badge

Hi, I need to call api to retrieve some transactions details from 3rd party application into acumatica erp. For testing, I used below code in my own customization project. This is a POST request and url is also defined there. 

public virtual async Task<IEnumerable> InsertSelectedLines(PXAdapter adapter)
{
foreach (PXResult<SOLine, SOOrder> result in POrdersView.Select())
{

if (lineExt.Selected1 == true)

{
APProformaItemList toBeInserted = new APProformaItemList();

toBeInserted.Ponbr = order.OrderNbr;
toBeInserted.LineNbr = lineNbr++;

var url = String.Format("https://sandbox.corporate-
api.hsbc.com/mock/v2/transactions");
HttpClient client= new HttpClient(); //added reference

client.DefaultRequestHeaders.Add("ContentType","
application/json");

client.DefaultRequestHeaders.Add("x-report-type", "JSON");

string data = "test=something";
//var stringContent = new StringContent(json, Encoding.UTF8,
"application/json");
StringContent queryString = new StringContent(data);
HttpResponseMessage response = await client.PostAsync(url,
queryString);
response.EnsureSuccessStatusCode();
string content = await response.Content.ReadAsStringAsync();
if (response.StatusCode == HttpStatusCode.OK)
{

toBeInserted.Description = "test if";
}
else
{
//throw new Exception(content);
toBeInserted.Description = " else";
}



}
}

}
return adapter.Get();

Above code defined inside a method which is used to add values from smart panel to custom screen details level. I just need to test whether this is working or not, that’s why I’m assigning some testing values to description field. But the time I adding values from the screen, I’m getting the error-

“Unable to cast object of type 'System.Threading.Tasks.Task`1[System.Collections.IEnumerable]' to type 'System.Collections.IEnumerable'”

Can someone help me to find a solution to this or provide me the guidance to get this  done?

Thank you.

icon

Best answer by sweta68 22 May 2023, 08:18

View original

6 replies

Userlevel 4
Badge

Hello, I believe the async in the method is causing this error. 

Userlevel 4
Badge

Can you write the code that is calling the method?

Userlevel 3
Badge

hi @anahizentella94 

thank you for the response. here is the code of calling the method, this method calls for the smart panel which I use to add data.

public virtual IEnumerable myAction(PXAdapter adapter)
{

try
{
if (POrdersView.AskExt() == WebDialogResult.OK)
{
//return InsertSelectedLines(adapter);
return (IEnumerable)InsertSelectedLines(adapter);
}
}

return adapter.Get();

 

Badge +11

Do you need to use an async Task?

 

Because if not, simply having it return an IEnumerable instead of Task<IEnumerable> should work.

Userlevel 3
Badge

@darylbowman 

yes I need to perform an async task, that’s why I have used async key in the definition. thank you.

Userlevel 7
Badge +10

Hi @oshadarodrigo64 

 

In the code snippet below, the method signature is being updated.

public virtual async Task<IEnumerable> InsertSelectedLines(PXAdapter adapter)
{
// Rest of your code...

await Task.CompletedTask; // Add this line if there are no other awaitable tasks in the method

return adapter.Get();
}

By using Task.CompletedTask, you can ensure that the method has an await able task, which allows the correct return type.
 

Hope it helps!

Regards,

Sweta

Reply


About Acumatica ERP system
Acumatica Cloud ERP provides the best business management solution for transforming your company to thrive in the new digital economy. Built on a future-proof platform with open architecture for rapid integrations, scalability, and ease of use, Acumatica delivers unparalleled value to small and midmarket organizations. Connected Business. Delivered.
© 2008 — 2024  Acumatica, Inc. All rights reserved