bgmulinari / B1SLayer

A lightweight SAP Business One Service Layer client for .NET
MIT License
138 stars 47 forks source link

Best way o check if item exist #76

Open CribAd opened 3 months ago

CribAd commented 3 months ago

Hi all,

Nice library, thanks!

I want to check if an Item with a specific ItemCode exists in SAP B1.

I do https://[servicelayer]/b1s/v1/Items('12345')?$select=ItemCode. When ItemCode 12345 exists, I will get a successful response. But when it does not exist I get an unhandled exception. Of course I can resolve this with a try-catch, but that doesn't sound like the most smooth way to check if a specific item exists. What is your advise?

jmartins-sh commented 3 months ago

Hey @CribAd how's it going? Hope well!

Actually you probably got a 404 status code, which will throw an exception. If you like you can modify this behavior during the request, using the extension method called .AllowHttpStatus(statusCode), in your case 404 (NotFound).

//public static SLRequest AllowHttpStatus(this SLRequest request, params HttpStatusCode[] statusCodes)

var item = await serviceLayer.Request("Item","12345").Select("ItemCode").AllowHttpStatus(HttpStatusCode.NotFound).GetAsync<MyItemModel>();

Using the extension method you prevent the exception in this case.

CribAd commented 2 months ago

Thanks, that works.