简体   繁体   English

使用Dynamics 365 Web API使用空备用键检索记录

[英]retrieve a record using Dynamics 365 Web API with empty alternate key

Problem 问题

I'm trying to query records from Dynamics 365 using the Web API where a key attribute is null. 我正在尝试使用key属性为null的Web API从Dynamics 365查询记录。

Approach 途径

For testing purposes I created an entity with a string attribute, an integer attribute, and a decimal attribute. 为了进行测试,我创建了一个具有字符串属性,整数属性和十进制属性的实体。 I then created an alternate key and put those three attributes in it, making this combination of attributes unique. 然后,我创建了一个备用键,并将这三个属性放入其中,从而使属性的组合变得唯一。 I then created some records. 然后,我创建了一些记录。

Querying the data using the Web API works as expected. 使用Web API查询数据的工作符合预期。 I created a record with those values: 我用这些值创建了一条记录:

{ "my_string_key": "s1", "my_integer_key": 1, "my_decimal_key": 1.23 }

And query it like this: 并像这样查询它:

/my_entities(my_string_key='s1',my_integer_key=1,my_decimal_key=1.23)

This returns the desired record. 这将返回所需的记录。

However I can't get it to work when any of the key fields is empty, it always returns a 400 with message "Bad Request - Error in query syntax". 但是,当任何关键字段为空时,我无法使其正常工作,它始终返回400,并显示消息“错误的请求-查询语法错误”。 Just for clarification: I purposely created records where one of the key attributes is empty, like this: 为了澄清起见:我特意创建了其中关键属性之一为空的记录,如下所示:

{ "my_integer_key": 1, "my_decimal_key": 1.23 } { "my_string_key": "s1", "my_decimal_key": 1.23 } { "my_string_key": "s1", "my_integer_key": 1 }

Notice how each record is missing one of the key attributes, which effectively leaves it at null. 请注意,每条记录是如何缺少关键属性之一的,这实际上使该记录为空。

I tried using =null , =empty , ='' , = and =Microsoft.OData.Core.ODataNullValue but nothing works. 我尝试使用=null=empty=''==Microsoft.OData.Core.ODataNullValue但没有任何效果。 I tried to query them like this: 我试图这样查询他们:

/my_entities(my_string_key=null,my_integer_key=1,my_decimal_key=1.23) /my_entities(my_string_key='s1',my_integer_key=null,my_decimal_key=1.23) /my_entities(my_string_key='s1',my_integer_key=1,my_decimal_key=null)

I also tried omitting the attribute with null in it, like this: 我还尝试省略其中带有null的属性,如下所示:

/my_entities(my_integer_key=1,my_decimal_key=1.23) /my_entities(my_string_key='s1',my_decimal_key=1.23) /my_entities(my_string_key='s1',my_integer_key=1)

None of this works. 这些都不起作用。 Can anyone provide a solution? 谁能提供解决方案?

Final Words 最后的话

I know that key attributes shouldn't be empty because that's not the point of key attributes. 我知道关键属性不应为空,因为这不是关键属性的重点。 My scenario however requires to handle this specific case. 但是,我的情况需要处理此特定情况。

I also know that I could just use the $filter parameter, but I'd like a solution without having to resort to this "workaround". 我也知道我可以只使用$ filter参数,但是我想一个解决方案而不必求助于这种“变通方法”。 After all, it is a key and I'd like to treat it as one. 毕竟,这是关键,我想将其视为一个关键。

I think your efforts prove that key attributes cannot be empty. 我认为您的努力证明了关键属性不能为空。 So, time for another approach. 所以,该换另一种方法了。

Create a custom attribute and fill it with the combined values of the three key attributes. 创建一个自定义属性,并用三个关键属性的组合值填充它。 You can do this in a plugin registered in the pre operation stage of the Create and eventually Update messages of your entity. 您可以在实体的“创建并最终更新”消息的操作前阶段注册的插件中执行此操作。

Use the custom attribute as the key attribute. 使用自定义属性作为键属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM