使用 OData 和 Python 在 Microsoft Dynamics 中创建销售订单和销售行

发布于 2025-01-11 06:24:17 字数 6454 浏览 0 评论 0 原文

我正在致力于将 Microsoft Dynamics NAV 2013 R2 与我的 Python 应用程序集成。我此集成的目标是:

  1. 查询所有销售订单
  2. 创建新的销售订单和相关销售行

在 Dynamics NAV 方面,我使用与 OData 1.0 - 3.0 兼容的 Web 服务。我从 Web 服务公开销售订单和销售行对象。我可以通过浏览器访问网络服务。

在 Python 方面,我使用 pyodata 与 Dynamics NAV 进行通信。

我的测试代码如下所示:

import pyodata
import requests
from requests_ntlm import HttpNtlmAuth
import logging

# Logging to get more information
Log_Format = "%(levelname)s %(asctime)s - %(message)s"
logging.basicConfig(level=logging.DEBUG, filename="logfile.log", filemode="w", format=Log_Format)
SERVICE_URL = "http://localhost:7648/RRFITest4/OData/"

# Creating session and providing authentication credentials
session = requests.Session()
session.auth = HttpNtlmAuth('username', 'password')
client = pyodata.Client(SERVICE_URL, session)

# Get the entity specific to the company name
company_entity = client.entity_sets.Company.get_entity('Company Name')
sales_order = {
    'Sell_to_Customer_No': '30001',
    'Sales_OrderSalesLines': {
        'Document_Type': 'Order',
        'Document_No': 'SO-0147924',
        'Line_No': 10000,
        'Type': "Item",
        'Family_Code': 'B116A',
        'No': 'W211608',
        'Quantity_Container': 0,
    }
}
# Creating a navigation property of the company
create_request = company_entity.nav('Sales_Order').create_entity()
create_request.set(**sales_order)
new_sales_order = create_request.execute()
print(new_sales_order)

我面临的问题是我可以很好地查询所有销售订单,但是当我尝试创建销售订单以及相关的销售行时,我收到以下错误:

Traceback (most recent call last):
  File "C:/Users/hsajib/Dev/nav-webservices-integration/odata-sample.py", line 37, in <module>
    new_sales_order = create_request.execute()
  File "C:\Users\UserName\AppData\Local\pypoetry\Cache\virtualenvs\nav-webservices-integration-iyrYYhSb-py3.8\lib\site-packages\pyodata\v2\service.py", line 305, in execute
    return self._handler(response)
  File "C:\Users\UserName\AppData\Local\pypoetry\Cache\virtualenvs\nav-webservices-integration-iyrYYhSb-py3.8\lib\site-packages\pyodata\v2\service.py", line 1135, in create_entity_handler
    raise HttpError('HTTP POST for Entity Set {0} failed with status code {1}'
pyodata.exceptions.HttpError: HTTP POST for Entity Set Sales_Order failed with status code 400

Process finished with exit code 1

这是详细的日志从运行代码:

INFO 2022-03-02 10:08:12,279 - Fetching metadata
DEBUG 2022-03-02 10:08:12,281 - Starting new HTTP connection (1): localhost:7648
DEBUG 2022-03-02 10:08:12,347 - http://localhost:7648 "GET /RRFITest4/OData/$metadata HTTP/1.1" 401 0
DEBUG 2022-03-02 10:08:12,367 - http://localhost:7648 "GET /RRFITest4/OData/$metadata HTTP/1.1" 401 0
DEBUG 2022-03-02 10:08:13,540 - http://localhost:7648 "GET /RRFITest4/OData/$metadata HTTP/1.1" 200 42231
DEBUG 2022-03-02 10:08:13,541 - Retrieved the response:
H: Cache-Control: no-cache
H: Content-Length: 42231
H: Content-Type: application/xml;charset=utf-8
H: Server: Microsoft-HTTPAPI/2.0
H: X-Content-Type-Options: nosniff
H: DataServiceVersion: 1.0;
H: Date: Wed, 02 Mar 2022 15:08:12 GMT
INFO 2022-03-02 10:08:13,542 - Creating OData Schema (version: 2)
INFO 2022-03-02 10:08:13,563 - Creating OData Service (version: 2)
DEBUG 2022-03-02 10:08:13,563 - New entity set proxy instance for Customers
DEBUG 2022-03-02 10:08:13,563 - New entity set proxy instance for CustomersDoc_Distribution_Line
DEBUG 2022-03-02 10:08:13,564 - New entity set proxy instance for Items
DEBUG 2022-03-02 10:08:13,564 - New entity set proxy instance for Job_Ledger_Entries
DEBUG 2022-03-02 10:08:13,564 - New entity set proxy instance for Sales_Lines
DEBUG 2022-03-02 10:08:13,564 - New entity set proxy instance for Sales_Order
DEBUG 2022-03-02 10:08:13,564 - New entity set proxy instance for Sales_OrderSalesLines
DEBUG 2022-03-02 10:08:13,564 - New entity set proxy instance for Company
DEBUG 2022-03-02 10:08:13,564 - Detected single property key, adding pair Name->Company Name to keyproperties
INFO 2022-03-02 10:08:13,564 - Getting entity Company for key Company Name and args {}
DEBUG 2022-03-02 10:08:13,564 - New instance of EntityGetRequest for last segment: Company
DEBUG 2022-03-02 10:08:13,564 - New entity set proxy instance for Sales_Order
DEBUG 2022-03-02 10:08:13,564 - New instance of EntityCreateRequest for entity type: Sales_Order on path Company('Company Name')/Sales_Order
INFO 2022-03-02 10:08:13,564 - {'Sell_to_Customer_No': '30001'}
DEBUG 2022-03-02 10:08:13,564 - Send (execute) POST request to http://localhost:7648/RRFITest4/OData/Company('Company Name')/Sales_Order
DEBUG 2022-03-02 10:08:13,564 -   query params: {}
DEBUG 2022-03-02 10:08:13,564 -   headers: {'Accept': 'application/json', 'Content-Type': 'application/json', 'X-Requested-With': 'X'}
DEBUG 2022-03-02 10:08:13,564 -   body: {"Sell_to_Customer_No": "30001"}
DEBUG 2022-03-02 10:08:13,646 - http://localhost:7648 "POST /RRFITest4/OData/Company('Company%20Name')/Sales_Order HTTP/1.1" 401 0
DEBUG 2022-03-02 10:08:13,705 - http://localhost:7648 "POST /RRFITest4/OData/Company('Company%20Name')/Sales_Order HTTP/1.1" 401 0
DEBUG 2022-03-02 10:08:13,755 - http://localhost:7648 "POST /RRFITest4/OData/Company('Company%20Name')/Sales_Order HTTP/1.1" 400 187
DEBUG 2022-03-02 10:08:13,755 - Received response
DEBUG 2022-03-02 10:08:13,755 -   url: http://localhost:7648/RRFITest4/OData/Company('Company%20Name')/Sales_Order
DEBUG 2022-03-02 10:08:13,756 -   headers: {'Content-Length': '187', 'Content-Type': 'application/json;odata=minimalmetadata;streaming=true;charset=utf-8', 'Server': 'Microsoft-HTTPAPI/2.0', 'X-Content-Type-Options': 'nosniff', 'DataServiceVersion': '3.0;', 'Date': 'Wed, 02 Mar 2022 15:08:13 GMT'}
DEBUG 2022-03-02 10:08:13,756 -   status code: 400
DEBUG 2022-03-02 10:08:13,756 -   body: {"odata.error":{"code":"","message":{"lang":"en-US","value":"The length of the string is 31, but it must be less than or equal to 30 characters. Value: ='Company Name'"}}}

我尝试从 NAV 端调试它。为此,我使用了 Dynamics NAV 调试器,并使用“调试下一步”选项。我已经使用了 这篇文章作为调试指南,但没有任何运气。

我的问题是如何解决此错误并为销售订单创建销售行?我还可以做其他事情来在 NAV 方面进行调试吗?

I am working on integrating Microsoft Dynamics NAV 2013 R2 with my Python application. My objectives for this integration are:

  1. Query all the Sales Orders
  2. Create a new Sales Order and related Sales Lines

On the Dynamics NAV side, I am using web services compatible with OData 1.0 - 3.0. I am exposing both Sales Order and Sales Line objects from the web service. I can access the web service through a browser.

On the Python side, I am using pyodata to communicate with Dynamics NAV.

My testing code looks like following:

import pyodata
import requests
from requests_ntlm import HttpNtlmAuth
import logging

# Logging to get more information
Log_Format = "%(levelname)s %(asctime)s - %(message)s"
logging.basicConfig(level=logging.DEBUG, filename="logfile.log", filemode="w", format=Log_Format)
SERVICE_URL = "http://localhost:7648/RRFITest4/OData/"

# Creating session and providing authentication credentials
session = requests.Session()
session.auth = HttpNtlmAuth('username', 'password')
client = pyodata.Client(SERVICE_URL, session)

# Get the entity specific to the company name
company_entity = client.entity_sets.Company.get_entity('Company Name')
sales_order = {
    'Sell_to_Customer_No': '30001',
    'Sales_OrderSalesLines': {
        'Document_Type': 'Order',
        'Document_No': 'SO-0147924',
        'Line_No': 10000,
        'Type': "Item",
        'Family_Code': 'B116A',
        'No': 'W211608',
        'Quantity_Container': 0,
    }
}
# Creating a navigation property of the company
create_request = company_entity.nav('Sales_Order').create_entity()
create_request.set(**sales_order)
new_sales_order = create_request.execute()
print(new_sales_order)

The problem I am facing is I can query all the sales orders just fine but when I am trying to create a sales order along with related sales lines, I am getting the following error:

Traceback (most recent call last):
  File "C:/Users/hsajib/Dev/nav-webservices-integration/odata-sample.py", line 37, in <module>
    new_sales_order = create_request.execute()
  File "C:\Users\UserName\AppData\Local\pypoetry\Cache\virtualenvs\nav-webservices-integration-iyrYYhSb-py3.8\lib\site-packages\pyodata\v2\service.py", line 305, in execute
    return self._handler(response)
  File "C:\Users\UserName\AppData\Local\pypoetry\Cache\virtualenvs\nav-webservices-integration-iyrYYhSb-py3.8\lib\site-packages\pyodata\v2\service.py", line 1135, in create_entity_handler
    raise HttpError('HTTP POST for Entity Set {0} failed with status code {1}'
pyodata.exceptions.HttpError: HTTP POST for Entity Set Sales_Order failed with status code 400

Process finished with exit code 1

Here are the detailed logs from running the code:

INFO 2022-03-02 10:08:12,279 - Fetching metadata
DEBUG 2022-03-02 10:08:12,281 - Starting new HTTP connection (1): localhost:7648
DEBUG 2022-03-02 10:08:12,347 - http://localhost:7648 "GET /RRFITest4/OData/$metadata HTTP/1.1" 401 0
DEBUG 2022-03-02 10:08:12,367 - http://localhost:7648 "GET /RRFITest4/OData/$metadata HTTP/1.1" 401 0
DEBUG 2022-03-02 10:08:13,540 - http://localhost:7648 "GET /RRFITest4/OData/$metadata HTTP/1.1" 200 42231
DEBUG 2022-03-02 10:08:13,541 - Retrieved the response:
H: Cache-Control: no-cache
H: Content-Length: 42231
H: Content-Type: application/xml;charset=utf-8
H: Server: Microsoft-HTTPAPI/2.0
H: X-Content-Type-Options: nosniff
H: DataServiceVersion: 1.0;
H: Date: Wed, 02 Mar 2022 15:08:12 GMT
INFO 2022-03-02 10:08:13,542 - Creating OData Schema (version: 2)
INFO 2022-03-02 10:08:13,563 - Creating OData Service (version: 2)
DEBUG 2022-03-02 10:08:13,563 - New entity set proxy instance for Customers
DEBUG 2022-03-02 10:08:13,563 - New entity set proxy instance for CustomersDoc_Distribution_Line
DEBUG 2022-03-02 10:08:13,564 - New entity set proxy instance for Items
DEBUG 2022-03-02 10:08:13,564 - New entity set proxy instance for Job_Ledger_Entries
DEBUG 2022-03-02 10:08:13,564 - New entity set proxy instance for Sales_Lines
DEBUG 2022-03-02 10:08:13,564 - New entity set proxy instance for Sales_Order
DEBUG 2022-03-02 10:08:13,564 - New entity set proxy instance for Sales_OrderSalesLines
DEBUG 2022-03-02 10:08:13,564 - New entity set proxy instance for Company
DEBUG 2022-03-02 10:08:13,564 - Detected single property key, adding pair Name->Company Name to keyproperties
INFO 2022-03-02 10:08:13,564 - Getting entity Company for key Company Name and args {}
DEBUG 2022-03-02 10:08:13,564 - New instance of EntityGetRequest for last segment: Company
DEBUG 2022-03-02 10:08:13,564 - New entity set proxy instance for Sales_Order
DEBUG 2022-03-02 10:08:13,564 - New instance of EntityCreateRequest for entity type: Sales_Order on path Company('Company Name')/Sales_Order
INFO 2022-03-02 10:08:13,564 - {'Sell_to_Customer_No': '30001'}
DEBUG 2022-03-02 10:08:13,564 - Send (execute) POST request to http://localhost:7648/RRFITest4/OData/Company('Company Name')/Sales_Order
DEBUG 2022-03-02 10:08:13,564 -   query params: {}
DEBUG 2022-03-02 10:08:13,564 -   headers: {'Accept': 'application/json', 'Content-Type': 'application/json', 'X-Requested-With': 'X'}
DEBUG 2022-03-02 10:08:13,564 -   body: {"Sell_to_Customer_No": "30001"}
DEBUG 2022-03-02 10:08:13,646 - http://localhost:7648 "POST /RRFITest4/OData/Company('Company%20Name')/Sales_Order HTTP/1.1" 401 0
DEBUG 2022-03-02 10:08:13,705 - http://localhost:7648 "POST /RRFITest4/OData/Company('Company%20Name')/Sales_Order HTTP/1.1" 401 0
DEBUG 2022-03-02 10:08:13,755 - http://localhost:7648 "POST /RRFITest4/OData/Company('Company%20Name')/Sales_Order HTTP/1.1" 400 187
DEBUG 2022-03-02 10:08:13,755 - Received response
DEBUG 2022-03-02 10:08:13,755 -   url: http://localhost:7648/RRFITest4/OData/Company('Company%20Name')/Sales_Order
DEBUG 2022-03-02 10:08:13,756 -   headers: {'Content-Length': '187', 'Content-Type': 'application/json;odata=minimalmetadata;streaming=true;charset=utf-8', 'Server': 'Microsoft-HTTPAPI/2.0', 'X-Content-Type-Options': 'nosniff', 'DataServiceVersion': '3.0;', 'Date': 'Wed, 02 Mar 2022 15:08:13 GMT'}
DEBUG 2022-03-02 10:08:13,756 -   status code: 400
DEBUG 2022-03-02 10:08:13,756 -   body: {"odata.error":{"code":"","message":{"lang":"en-US","value":"The length of the string is 31, but it must be less than or equal to 30 characters. Value: ='Company Name'"}}}

I have tried to debug this from the NAV side. For that I have used the Dynamics NAV debugger using the 'Debug Next' option. I have used this article as a guideline for debugging without any luck.

My question is how can I get around this error and create the sales lines for the sales order? Are there other things I can do to debug this on the NAV side?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

新雨望断虹 2025-01-18 06:24:17

日志的最后一行

DEBUG 2022-03-02 10:08:13,756 -   body: {"odata.error":{"code":"","message":{"lang":"en-US","value":"The length of the string is 31, but it must be less than or equal to 30 characters. Value: ='Company Name'"}}}

清楚地表明您在公司名称参数中传递的值超过 30 个字符,超出了 Navision 端公司名称字段的字符长度。

the last line of your log

DEBUG 2022-03-02 10:08:13,756 -   body: {"odata.error":{"code":"","message":{"lang":"en-US","value":"The length of the string is 31, but it must be less than or equal to 30 characters. Value: ='Company Name'"}}}

clearly tells that the value you are passing in Company Name parameter have more than 30 characters which exceeds the character length of Company Name field in Navision side.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文