如何使用 Business Central API 2.0 创建联系人?

发布于 2025-01-11 16:43:41 字数 4880 浏览 6 评论 0原文

(注意:下面提到的文档在提交时是错误的。看起来它是从模板复制的并且没有更改。我已在 Microsoft 的 GitHub 页面上提交了评论。)

有没有人成功创建了使用 Business Central v2 API 进行联系?我正在遵循文档 这里并且没有任何成功。更新效果很好,但我根本无法让创建请求工作。

该文档说我应该能够像这样发布到联系人端点,

POSTbusinesscentralPrefix/companies({id})/contacts({id})

事实上 {id} 用作 companiescontacts URL 组件的占位符很奇怪,根本不是我所期望的。该页面上还给出了一个更完整的示例:

POST https://{businesscentralPrefix}/api/v2.0/companies({id})/contacts({id})
Content-type: application/json
{
    "id" : "5d115c9c-44e3-ea11-bb43-000d3a2feca1",
    "number" : "108001",
    "type" : "Company",
    "displayName": "CRONUS USA, Inc.",
    "companyNumber" : "17806",
    "companyName" : "CRONUS US",
    "businessRelation" : "Vendor",
    "addressLine1": "7122 South Ashford Street",
    "addressLine2": "Westminster",
    "city": "Atlanta",
    "state": "GA",
    "country": "US",
    "postalCode": "31772",
    "phoneNumber": "+1 425 555 0100",
    "mobilePhoneNumber" : "",
    "email" : "[email protected]",
    "website" : "",
    "searchName" : "",
    "privacyBlocked" : true,
    "lastInteractionDate" : "2021-06-01",
    "lastModifiedDateTime" : "2021-06-01"
}

该示例在有效负载中有一个 id 属性,这似乎不是我应该创建的东西。考虑到 URL 中存在重复的 {id} 占位符,这里的 id 再次令人困惑。

此外,还有一些标头要求对于创建请求没有意义:

If-Match 必需。当包含此请求标头并且提供的 eTag 与联系人上的当前标签不匹配时,将不会更新联系人。

如果我创建联系人,则不会有 etag,因此该标头似乎不适用于创建请求。如果是这样的话,那么可能就不能太依赖文档了。如果是这样的话,那么我不禁想知道创建端点是否不应该是:

POST https://{businesscentralPrefix}/api/v2.0/companies ({company-guid})/contacts

这似乎与我遇到的其他 REST API 更一致,但让我想知道是否需要为新联系人提供 id ?我的回答是“不”,但 Microsoft 的文档在示例之外并未提及这一点。

更新现有联系人没有任何问题。我只剩下三个选项来创建一个:

  1. POST https://{businesscentralPrefix}/api/v2.0/companies({company-guid})/contacts({company-guid})< /代码>

    这就是文档所暗示的内容,但考虑到您正在通过公司 ID 有效地过滤联系人表,它没有任何意义。我只是为了它而尝试

POST https://{businesscentralPrefix}/api/v2.0/companies({company-guid})/contacts({company-guid})
{
    "id":"8adc4ec5-8393-44ac-8860-fadd9e3603cb",
    "number": "TEST123",
    "displayName": "Another Test Contact",
    "type": "Person",
    ...
}
...
Response (with and without the contact guid in payload)
{
    "error": {
        "code": "BadRequest_MethodNotAllowed",
        "message": "'POST' requests for 'contacts' of EdmType 'Entity' are not allowed within Dynamics 365 Business Central OData web services.  CorrelationId:  XXX"
    }
}
  1. POST https://{businesscentralPrefix}/api/v2.0/companies({company-guid})/contacts({contact-guid})< /代码>

    这看起来也很奇怪,因为我似乎不应该创建记录的 ID。也只是为了尝试一下:

POST https://api.businesscentral.dynamics.com/v2.0/{tenent-guid}/{environment}/api/v2.0/companies({company-guid})/contacts(8adc4ec5-8393-44ac-8860-fadd9e3603cb)
{
    "id":"8adc4ec5-8393-44ac-8860-fadd9e3603cb",
    "number": "TEST123",
    "displayName": "Another Test Contact",
    "type": "Person",
    ...
}
...
Response (with and without the contact id guid in payload)
{
    "error": {
        "code": "BadRequest_MethodNotAllowed",
        "message": "'POST' requests for 'contacts' of EdmType 'Entity' are not allowed within Dynamics 365 Business Central OData web services.  CorrelationId:  XXXX."
    }
}
  1. POST https://{businesscentralPrefix}/api/v2.0/companies({company-guid})/contacts

    数字 3 在我看来是有道理的,但失败了

POST https://api.businesscentral.dynamics.com/v2.0/{tenent-guid}/{environment}/api/v2.0/companies({company-guid})/contacts(8adc4ec5-8393-44ac-8860-fadd9e3603cb)

{
    "id":"8adc4ec5-8393-44ac-8860-fadd9e3603cb",
    "number": "TEST123",
    "displayName": "Another Test Contact",
    "type": "Person",
    ...
}
...
Response (with and without the contact id guid in payload)
{
    "error": {
        "code": "Internal_RecordNotFound",
        "message": "The Contact does not exist. Identification fields and values: No.='TEST123'  CorrelationId:  XXX."
    }
}

有没有人成功创建联系人使用业务中心 v2 API?如果是这样,你是怎么做到的以及我做错了什么?另外,我正在使用的系统是从本地 NAV 实例 fwiw 升级的。

(NOTE: The documentation mentioned below is wrong at the time of this submission. It looks like it was copied from a template and not changed. I've submitted comment on Microsoft's GitHub page.)

Has anyone had success creating a contact using the Business Central v2 API? I'm following the documentation here and not having any success. Updates work great, but I can't get create requests working at all.

The documentation says I should be able to post to the contacts end-point like so,

POST businesscentralPrefix/companies({id})/contacts({id})

The fact that {id} is used as a placeholder for both companies and contacts URL components is strange and not at all what I would expect. A more complete example is also given on that page:

POST https://{businesscentralPrefix}/api/v2.0/companies({id})/contacts({id})
Content-type: application/json
{
    "id" : "5d115c9c-44e3-ea11-bb43-000d3a2feca1",
    "number" : "108001",
    "type" : "Company",
    "displayName": "CRONUS USA, Inc.",
    "companyNumber" : "17806",
    "companyName" : "CRONUS US",
    "businessRelation" : "Vendor",
    "addressLine1": "7122 South Ashford Street",
    "addressLine2": "Westminster",
    "city": "Atlanta",
    "state": "GA",
    "country": "US",
    "postalCode": "31772",
    "phoneNumber": "+1 425 555 0100",
    "mobilePhoneNumber" : "",
    "email" : "[email protected]",
    "website" : "",
    "searchName" : "",
    "privacyBlocked" : true,
    "lastInteractionDate" : "2021-06-01",
    "lastModifiedDateTime" : "2021-06-01"
}

The example has an id property in the payload, which doesn't seem like something I should be creating. Again the id here is confusing given the duplicate {id} placeholders in the URL.

Additionally, there are some header requirements that don't make sense for a create request:

If-Match Required. When this request header is included and the eTag provided does not match the current tag on the contact, the contact will not be updated.

I won't have an etag if I'm creating a contact, so that header doesn't seem to apply to create requests. If that's the case, then probably can't rely much on the documentation. If that's the case, then I can't help but wonder if the create end-point shouldn't be:

POST https://{businesscentralPrefix}/api/v2.0/companies({company-guid})/contacts

which seems more consistent with other REST APIs I've encountered, but leaves me wondering whether or not I need supply the id for the new contact? I'm going with "no", but Microsoft's documentation doesn't mention it outside of the examples.

I have no problems updating an existing contact. I'm left with three options for creating one:

  1. POST https://{businesscentralPrefix}/api/v2.0/companies({company-guid})/contacts({company-guid})

    This one is what the docs imply, but it doesn't make any sense given that you're effectively filtering the contacts table by a company id. I gave it a shot just for the sake of it

POST https://{businesscentralPrefix}/api/v2.0/companies({company-guid})/contacts({company-guid})
{
    "id":"8adc4ec5-8393-44ac-8860-fadd9e3603cb",
    "number": "TEST123",
    "displayName": "Another Test Contact",
    "type": "Person",
    ...
}
...
Response (with and without the contact guid in payload)
{
    "error": {
        "code": "BadRequest_MethodNotAllowed",
        "message": "'POST' requests for 'contacts' of EdmType 'Entity' are not allowed within Dynamics 365 Business Central OData web services.  CorrelationId:  XXX"
    }
}
  1. POST https://{businesscentralPrefix}/api/v2.0/companies({company-guid})/contacts({contact-guid})

    this one also seems weird since it doesn't seem like I should be creating the record's id. Also tried it just to try it:

POST https://api.businesscentral.dynamics.com/v2.0/{tenent-guid}/{environment}/api/v2.0/companies({company-guid})/contacts(8adc4ec5-8393-44ac-8860-fadd9e3603cb)
{
    "id":"8adc4ec5-8393-44ac-8860-fadd9e3603cb",
    "number": "TEST123",
    "displayName": "Another Test Contact",
    "type": "Person",
    ...
}
...
Response (with and without the contact id guid in payload)
{
    "error": {
        "code": "BadRequest_MethodNotAllowed",
        "message": "'POST' requests for 'contacts' of EdmType 'Entity' are not allowed within Dynamics 365 Business Central OData web services.  CorrelationId:  XXXX."
    }
}
  1. POST https://{businesscentralPrefix}/api/v2.0/companies({company-guid})/contacts

    Number 3 makes sense in my mind but fails with

POST https://api.businesscentral.dynamics.com/v2.0/{tenent-guid}/{environment}/api/v2.0/companies({company-guid})/contacts(8adc4ec5-8393-44ac-8860-fadd9e3603cb)

{
    "id":"8adc4ec5-8393-44ac-8860-fadd9e3603cb",
    "number": "TEST123",
    "displayName": "Another Test Contact",
    "type": "Person",
    ...
}
...
Response (with and without the contact id guid in payload)
{
    "error": {
        "code": "Internal_RecordNotFound",
        "message": "The Contact does not exist. Identification fields and values: No.='TEST123'  CorrelationId:  XXX."
    }
}

Has anyone had success creating a contact using the Business Central v2 API? If so, how did you do it and what am I doing wrong? Also, the system I'm working with was upgrade from a local NAV instance, fwiw.

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

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

发布评论

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

评论(1

┊风居住的梦幻卍 2025-01-18 16:43:41

当有效负载中同时包含 numbertype 时,似乎会发生该错误。

解决方案是创建不带 numbertype 的联系人,然后通过补丁请求更新您遗漏的值。

The error seems to occur when both number and type is included in the payload.

The solution would be to create the contact without either number or type and then update the value you left out with a patch request.

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