0
votes

I'm trying to create Sales Orders and Invoices via Acumatica ERP's REST API. For some reason I'm getting error only for creation of the Invoices through I'm using almost the same JSON's for both:

Here is my RestService:

public class RestService : IDisposable
{
    private readonly HttpClient _httpClient;

    private readonly string _acumaticaBaseUrl;

    #region Ctor
    public RestService(string acumaticaBaseUrl, string userName, string password, string company, string branch, string locale)
    {
        _acumaticaBaseUrl = acumaticaBaseUrl;
        _httpClient = new HttpClient(
            new HttpClientHandler
            {
                UseCookies = true,
                CookieContainer = new CookieContainer()
            })
        {
            BaseAddress = new Uri(acumaticaBaseUrl + "/entity/Default/6.00.001/"),
            DefaultRequestHeaders =
            {
                Accept = {MediaTypeWithQualityHeaderValue.Parse("text/json") }
            }
        };

        //Log in to Acumatica ERP
        _httpClient.PostAsJsonAsync(
          acumaticaBaseUrl + "/entity/auth/login", new
          {
              name = userName,
              password = password,
              company = company,
              branch = branch,
              locale = locale
          }).Result
            .EnsureSuccessStatusCode();
    }



    void IDisposable.Dispose()
    {
        _httpClient.PostAsync(_acumaticaBaseUrl + "/entity/auth/logout",
          new ByteArrayContent(new byte[0])).Wait();
        _httpClient.Dispose();
    }
    #endregion
    //Data submission
    public string Put(string entityName, string parameters, string entity)
    {
        var res = _httpClient.PutAsync(_acumaticaBaseUrl + "/entity/Default/6.00.001/" + entityName + "?" + parameters, new StringContent(entity, Encoding.UTF8, "application/json")).Result
            .EnsureSuccessStatusCode();
        return res.Content.ReadAsStringAsync().Result;
    }
}

And here is the Main code:

using (RestService service = new RestService(ACUMATICA_INSTANCE_URL, USERNAME,PASSWORD,COMPANY, "", "EN-US"))
{
    Console.WriteLine("Login successful");

    string order = @"{  
        ""OrderNbr"":{ value: ""000204"" },
        ""CustomerID"":{ value: ""TESTCST005""},
        ""Details"": [ 
        {
            ""InventoryID"": { value: ""301CMPST02"" },
            ""Quantity"": { value: 10 }
        }]
    }";
    string invoice = @"{  
        ""ReferenceNbr"":{ value: ""001032"" },
        ""CustomerID"":{ value: ""TESTCST005""},
        ""Details"": [ 
        {
            ""InventoryID"": { value: ""DESIGN"" },
            ""Quantity"": { value: 10 }
        }]
    }";

    try
    {
        Console.WriteLine("Trying to create the following Order");
        Console.WriteLine(order);
        string updatedOrder = service.Put("SalesOrder", null, order);
        Console.WriteLine("Order created successful");
        Console.WriteLine(updatedOrder);

        Console.WriteLine("Trying to create the following Invoice");
        Console.WriteLine(invoice);
        string updatedInvoice = service.Put("SalesInvoice", null, invoice);
        Console.WriteLine("Invoice created successful");
        Console.WriteLine(updatedInvoice);
    }
    catch(Exception exc)
    {
        Console.WriteLine(exc.Message);
    }
    Console.ReadLine();
}

I'm not able to find out what is the problem. The response I'm getting is always the following:

"StatusCode: 500, ReasonPhrase: 'Internal Server Error', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:\r\n{\r\n X-Handled-By: Acumatica-PX.Export/AuthenticationManagerModule\r\n Cache-Control: private\r\n Set-Cookie: Locale=TimeZone=GMTM0500G&Culture=en-US; path=/\r\n Set-Cookie: UserBranch=5; path=/\r\n Server: Microsoft-IIS/8.5\r\n X-Powered-By: ASP.NET\r\n Date: Wed, 02 Aug 2017 13:02:49 GMT\r\n Content-Length: 36\r\n Content-Type: text/json; charset=utf-8\r\n}"

I'm able to add the Invoice from Screen with these same values. Here is the example of using Acumatica ERP REST API to Create a Customer Creation of a Record.

2
What is in the content of the response? It should contain error description. - Serg Rogovtsev
@SergRogovrsev it has only Internal Server Error - Samvel Petrosov
@SergRogovtsev take.ms/sBOEFn - Samvel Petrosov
@SergRogovtsev I have read the content and the message is "{\"message\":\"An error has occurred.\"}" - Samvel Petrosov
What does FirstChanceExceptionLog say? - Serg Rogovtsev

2 Answers

0
votes

I did not have your "PostAsJsonAsync" function so I had to revert to the method used in the documentation provided from Acumatica and use the following :

string credentialsAsString = JsonConvert.SerializeObject(new
{
    name = userName,
    password = password,
    //company = Properties.Settings.Default.CompanyName,
    //branch = Properties.Settings.Default.Branch
});

var response = _httpClient.PostAsync(acumaticaBaseUrl + "/entity/auth/login", new StringContent(credentialsAsString, Encoding.UTF8, "application/json")).Result;

But once i did this the only error I found was a JSON structure error. You are forgetting to put the value keyword between double quotes.

This :

string order = @"{  
    ""OrderNbr"":{ value: ""000204"" },
    ""CustomerID"":{ value: ""TESTCST005""},
    ""Details"": [ 
    {
        ""InventoryID"": { value: ""301CMPST02"" },
        ""Quantity"": { value: 10 }
    }]
}";

Should be corrected by this :

string order = @"{  
    ""OrderNbr"":{ ""value"": ""000204"" },
    ""CustomerID"":{ ""value"": ""TESTCST005""},
    ""Details"": [ 
    {
        ""InventoryID"": { ""value"": ""301CMPST02"" },
        ""Quantity"": { ""value"": 10 }
    }]
}";
0
votes

I got the same issue and resolved.

Issue is that Customer "TESTCST005" does not exist in the system. You need to create a new customer :)