【问题标题】:Acumatica REST API SalesInvoiceAcumatica REST API 销售发票
【发布时间】:2017-08-01 13:05:01
【问题描述】:

我正在尝试通过 Acumatica ERP 的 REST API 创建销售订单和发票。 出于某种原因,我只在创建发票时遇到错误,因为我对两者都使用了几乎相同的 JSON:

这是我的 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;
    }
}

这里是主要代码:

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();
}

我无法找出问题所在。我得到的响应总是如下:

"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 服务器:Microsoft-IIS/8.5\r\n X-Powered-By:ASP.NET\r\n 日期:2017 年 8 月 2 日星期三 13:02:49 GMT\r\n 内容长度:36\ r\n Content-Type: text/json; charset=utf-8\r\n}"

我可以使用这些相同的值添加来自屏幕的发票。 这是使用 Acumatica ERP REST API 创建客户的示例 Creation of a Record.

【问题讨论】:

  • 响应的内容是什么?它应该包含错误描述。
  • @SergRogovrsev 它只有内部服务器错误
  • @SergRogovtsev take.ms/sBOEFn
  • @SergRogovtsev 我已经阅读了内容,消息是"{\"message\":\"发生错误。\"}"
  • FirstChanceExceptionLog 说什么?

标签: c# webforms acumatica


【解决方案1】:

我没有您的“PostAsJsonAsync”功能,所以我不得不恢复到 Acumatica 提供的文档中使用的方法并使用以下方法:

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;

但是一旦我这样做了,我发现的唯一错误是 JSON 结构错误。 您忘记将 value 关键字放在双引号之间。

这个:

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

应该这样改正:

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

【讨论】:

  • 我在创建销售订单方面没有任何问题,PostAsyncAsJson 也是来自 net.formatters 的方法。我稍后会添加指向我使用过的文档的链接
  • 您的回答根本没有回答我的问题。我对 PostAsyncAsJson 没有任何问题,我对销售订单的 JSON 也没有任何问题
【解决方案2】:

我遇到了同样的问题并解决了。

问题是系统中不存在客户“TESTCST005”。 你需要创建一个新客户:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-24
    • 1970-01-01
    • 2013-03-11
    相关资源
    最近更新 更多