针对D365 F&O (Finance & Operations)的OData 查询选项

123 阅读1分钟

最近的迁移项目需要对比一下三个F&O环境的数据一致性.我试着用Postman 来构建我需要的数据集合. 

我希望用T-SQL的语句来应对OData的查询:(以下以VendorBankAccount为例)

SELECT VendorAccountNumber,
       VendorBankAccountId,
       BankAccountNumber,
       BankName,SWIFTCode
FROM VendorBankAccount
WHERE dataAreadId = 'CN02'
AND VendoAccountNumber LIKE '%VEN'
AND cross-company='True'

OData查询的组合基本符合FROM table/entity WHERE clause SELECT list.但是很多常用的命令在F&O这里貌似不好使, 可用的查询选项如下:

    • $filter
    • $count
    • $orderby
    • $skip
    • $top
    • $expand (only first-level expansion is supported)
    • $select

假设FO的环境包含多个公司,为何查询到所需的数据,我们需要使用cross-company=true来指定.

默认情况下,OData 仅返回属于用户默认公司的数据。要查看用户默认公司以外的数据,请指定?cross-company=true查询选项。此选项将返回用户有权访问的所有公司的数据. 我的示例中假设我的账户在CN01,我需要的数据在CN02, cross-company=true是首先要指定的选项,这个查询选项也可以放在filter的后面,但是我更倾向于把它直接放在查询对象的后面:

/data/VendorBankAccounts?cross-company=true

接下来就是filter(where)选项,如果你知道数据在哪个公司下,可以使用dataAreaId, 以我的为例:

&$filter=dataAreaId eq 'CN02'

然后可以添加更多个条件来查询数据:

&$filter=(dataAreaId eq 'CN02' and VendorAccountNumber eq '*VEN')

微软的官方文档对于使用Contains选项描述如下:

You can also use the Contains option with filterrequests.Ithasbeenimplementedasawildcardcharacter.Forexample: http://host/service/EntitySet?filter requests. It has been implemented as a wildcard character. For example: `http://host/service/EntitySet?filter=StringField eq 'retail'` The operators 'has' and 'in' are not supported.

我这里的示例 eq ‘*VEN'可以等效为 LIKE '%VEN'.但是这里并没有说 ne '*VEN'这类操作可行,据我所测试的结果不好使.'has' 和 'in' 也不支持.枚举类型位于命名空间Microsoft.Dynamics.DataEntities下。可以使用以下语法将枚举包含在OData 查询中.

Microsoft.Dynamics.DataEntities.Gender'Unknown'

Microsoft.Dynamics.DataEntities.NoYes'Yes'

下面显示了使用上述枚举值的示例查询.

/data/CustomersV3?$filter=PersonGender eq Microsoft.Dynamics.DataEntities.Gender'Unknown'
/data/Currencies?$filter=ReferenceCurrencyForTriangulation eq Microsoft.Dynamics.DataEntities.NoYes'No'

枚举支持的操作是eq和ne.

expand只支持一级扩展, 类似于只能join一个表/实体, 而且expand的对象必须在metadata里的对象中明确定义为NavigationPropertyBinding.

 <EntitySet Name="VendorsV2" EntityType="Microsoft.Dynamics.DataEntities.VendorV2">   <Navigation\propertyBinding Path="VendorBankAccounts" Target="VendorBankAccounts" />>

完整的GET示例如下:

{{FO_Prod}}/data/VendorBankAccounts?cross-company=true&$filter=(dataAreaId eq 'CN02' and VendorAccountNumber eq '*VEN')&$select=VendorAccountNumber,VendorBankAccountId,BankAccountNumber,BankName,SWIFTCode

总体来讲,F&O的OData查询选项是相对受限的集合,有可能与性能和安全性有关.