You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用VBA调用Dynamics 365 Web API更新记录,无主键时能否用其他字段?

在Dynamics 365 Web API中用非主键字段定位并更新记录(VBA实现)

核心方案说明

Dynamics 365 Web API不支持直接在PATCH请求中用$filter定位记录,但可以通过两种方式解决:

  1. 先查询获取主键GUID:用唯一字段组合作为过滤条件查询记录,拿到GUID后再执行更新
  2. 使用替代键(Alternate Key):如果这些唯一字段已在Dynamics 365中配置为替代键,可直接用替代键值定位记录,无需查询GUID

方案1:先查询获取GUID再更新

假设你的唯一字段是productid和pricelevelid(请替换为实际字段名),先执行查询拿到目标记录的GUID,再代入更新请求:

Dim crm As New crm
Dim queryReq As New WebRequest
Dim queryResp As WebResponse
Dim updateReq As New WebRequest
Dim updateResp As WebResponse
Dim dict As New Scripting.Dictionary
Dim jsonResp As Object

' 第一步:查询目标记录的GUID
queryReq.Resource = "productpricelevels?$select=productpricelevelid&$filter=productid eq 'PROD-001' and pricelevelid eq 'PL-001'" ' 替换为你的唯一字段和对应值
queryReq.Format = WebFormat.Json
queryReq.ResponseFormat = Json
queryReq.Method = WebMethod.HttpGet

Set queryResp = crm.Query(queryReq)
If queryResp.StatusCode = WebStatusCode.OK Then
    Set jsonResp = WebHelpers.ConvertFromJson(queryResp.Body)
    ' 检查是否找到唯一匹配记录
    If jsonResp.value.Count = 1 Then
        Dim targetGuid As String
        targetGuid = jsonResp.value(0).productpricelevelid
        
        ' 第二步:执行更新请求
        updateReq.Resource = "productpricelevels(" & targetGuid & ")"
        updateReq.Format = WebFormat.Json
        updateReq.ResponseFormat = Json
        updateReq.Method = WebMethod.HttpPatch
        
        ' 设置要更新的字段
        dict.Add "amount", 4
        updateReq.Body = WebHelpers.ConvertToJson(dict)
        
        Set updateResp = crm.Query(updateReq)
        If updateResp.StatusCode = WebStatusCode.NoContent Then
            Debug.Print "更新成功"
        Else
            Debug.Print "更新失败:" & updateResp.Body
        End If
    ElseIf jsonResp.value.Count = 0 Then
        Debug.Print "未找到匹配的记录"
    Else
        Debug.Print "找到多条匹配记录,请检查过滤条件的唯一性"
    End If
Else
    Debug.Print "查询失败:" & queryResp.Body
End If

方案2:使用替代键直接定位(需提前配置)

如果你已经在Dynamics 365中将那组唯一字段配置为替代键(比如为productpricelevels实体创建了productid+pricelevelid的组合替代键),可以直接在Request.Resource中用替代键值组合定位记录:

Dim crm As New crm
Dim Request As New WebRequest
Dim Response As WebResponse
Dim dict As New Scripting.Dictionary

' 用替代键定位记录,格式:实体名(替代键字段1='值1',替代键字段2='值2')
Request.Resource = "productpricelevels(productid='PROD-001',pricelevelid='PL-001')" ' 替换为你的替代键字段和对应值
Request.Format = WebFormat.Json
Request.ResponseFormat = Json
Request.Method = WebMethod.HttpPatch

' 设置要更新的字段
dict.Add "amount", 4
Request.Body = WebHelpers.ConvertToJson(dict)

Set Response = crm.Query(Request)
If Response.StatusCode = WebStatusCode.NoContent Then
    Debug.Print "更新成功"
Else
    Debug.Print "更新失败:" & Response.Body
End If

注意事项

  • 请替换代码中的字段名和对应值为你实际的唯一字段信息
  • 确保过滤条件能唯一匹配一条记录,避免更新错误的记录
  • 配置替代键需要Dynamics 365系统管理员权限,配置后需等待系统同步生效

内容的提问来源于stack exchange,提问作者David Greer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 15:11:12