Web API IC Adjustment posting handle Sage 300 errors

SOLVED

Hi,

I am very new to using the Web API integration in Sage 300. I am in the process of writing a system that will integrate IC Adjustments using the Web API and so far all seems to be working as expected.

One question I have:

If the API returns an error (there could be many: e.g. location is inactive, or item is inactive, etc.), how do I grab that error so that I handle it?

  • +1
    verified answer

    What language are you using to write to the web API?

    There are a couple of things really. When you submit to the web API you can query the http status code on the return. If it is in the 200 range, it's ok, 300s I think are to do with redirects and temporary unavailability. 400s are not available for one reason or other and 500s are general errors. You can find the status codes easily on the web.

    When you get that, you will also get some JSON returned as an error. Examples of the web API submit are here. In general using that as an example, when you submit using something like the SubmitRequest you get an object back which, in C# you can refer to with a dynamic object. You can query that to see if it has the response structure you want. If not, wrap it in a try/catch block and throw an error and pick up the object as an error response. The error response structure is the same across the API as far as I know:

    {
      "error": {
        "code": "General",
        "message": {
          "lang": "en-US",
          "value": "I/C Options.\r\n\r\nRecord has been modified by another program."
    
        }
      }
    }

    To convert that to a class (assuming you use C#) try this. Or you can create a new class in your project, delete the class code that it creates, copy the JSON, and then in Visual Studio, go to Edit -> Paste Special -> Paste JSON as Classes and it will convert it for you.

  • 0 in reply to Vega

    Thanks Vega. I am using C# to write to the Web API. The error example is exactly the response I get. I will work through the examples provided. 

  • 0 in reply to Vega

    I seem to be struggling a lot with this. I get the below response from the API:

    {
      "error": {
        "code": "General",
        "message": {
          "lang": "en-US",
          "value": "There is insufficient quantity available to adjust item BHE0001 at location ESHOP."
        }
      }
    }

    Basically, I am creating a dynamic object to send the request to the API. The request then grabs the response (the JSON above) and send it back to the dynamic object. How do I then query the dynamic object to know that there is an error and I need to do something with this error?

  • +1 in reply to Eddie Willcox_1
    verified answer

    I created a SageError class that defines the error JSON structure. I create that in the SendRequest method. When you send the data to the endpoint in Sage, you can pick up the response in the responsePayload as a string. Then switch on the (int)response.StatusCode to get the http status code from Sage. If you have an error status code, then set the responsePayload to by a serialised string of the error status which you can return. If not, return the serialised dynamic object. You can set an output variable in the SendRequest parameters to return the status code which you can query after SendRequest returns. That way you can query the response code from Sage and have an if statement that queries the dynamic error object or the dynamic IC adjustment. You query it by doing something like:

    string sAPBatchNumber = "";
    string sErrorMessage = "";
    
    dynamic sAPBatch = await SendRequest(HttpMethod.Post, sFullURI, "InsertAPBatch", ap, out int httpStatus).ConfigureAwait(false);
    
    if(httpsStatus < 300)
    {
      sAPBatchNumber = sAPBatch.BatchNumber;
    }
    else
    {
      sErrorMessage = sAPBatch.Error.Message.Value;
    }

    I write this off the top of my head so it may not be syntactically correct, but you get the idea.