通过Web API调用Action时各种类型输入参数传递值的方法
本人微信公众号:微软动态CRM专家罗勇 ,回复280或者20180906可方便获取本文,同时可以在第一间得到我发布的最新博文信息,follow me!我的网站是 www.luoyong.me 。
Dynamics 365的Web API功能很强大也越来越强大,流程的一种操作(Action)的功能也很强大也越来越强大(有个特征很好用,就是这些步骤默认组成一个事物,要么都成功,要么都失败)。
工作流中可以调用操作,通过Web API也可以方便的调用。
最近操作的参数增加了Entity这种类型,调用的时候如何赋值呢?我下面这个操作有两个复杂点的输入参数,一个类型为EntityReference的输入参数,一个类型为Entity的输入参数,还有一个类型为EntityReference的输出参数。

为啥要用到Entity类型的参数?典型场景就是我的步骤中需要用到这个Entity记录的非主键,非主属性字段的值,操作中某些字段赋值要用同类型,所以这时候就用上了。
为啥要用EntityReference类型的参数?给某个查找类型的字段赋值就要用到这种参数。
输出参数为EntityReference啥时候用到?你要获取到某个记录的ID时候,比如操作中创建的记录的ID。
官方有篇文章 Use Web API actions 介绍了Entity类型参数的赋值,可以参考,我这里汇总下调用方法如下:
var clientURL = Xrm.Page.context.getClientUrl();
var req = new XMLHttpRequest()
req.open("POST", encodeURI(clientURL + "/api/data/v8.2/new_lytests(93EDAC5A-ACA6-E811-8ACE-005056948ABE)/Microsoft.Dynamics.CRM.new_TestbyLuoYong"), true);
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.onreadystatechange = function () {
if (this.readyState == 4 /* complete */) {
req.onreadystatechange = null;
if (this.status == 200) {
//注意EntityReference类型输出参数的值是记录的主键值
Xrm.Utility.alertDialog(JSON.parse(this.response).new_workorderid);
}
else {
var error = JSON.parse(this.response).error;
Xrm.Utility.alertDialog("Error." + error.message);
}
}
};
var requestmsg = {};
//Entity类型的参数建议指定@data.type (可以不指定,若是lookup到具体实体,不是customer,partylist类型),格式为Microsoft.Dynamics.CRM.实体逻辑名称
//需要指定记录主键值,还有需要用到的该记录的其他字段值
requestmsg.WorkOrderType = {};
requestmsg.WorkOrderType.new_subjectid='1377FF4A-B494-E811-8ACD-005056948ABE';
requestmsg.WorkOrderType["@data.type"]="Microsoft.Dynamics.CRM.new_subject";
requestmsg.WorkOrderType.new_optionsetfielvalue = 1;
//EntityReference类型的参数指定记录ID就可以
requestmsg.ASP={};
requestmsg.ASP.accountid='FC96BE10-63AF-E811-8ACE-005056948ABE';
req.send(JSON.stringify(requestmsg));
对于参数类型为EntityCollection 类型参数的赋值,可以参考文章 Execute Action with “EntityCollection” Parameter using Web API in Dynamics 365 ,其实就是一个Entity类型参数的数组嘛。
我这里也摘录下官方的原文供各位参考:
When an action requires an entity as a parameter and the type of entity is ambiguous, you must use the @odata.type property to specify the type of entity. The value of this property is the fully qualified name of the entity, which follows this pattern: Microsoft.Dynamics.CRM.+<entity logical name>.
As shown in the Bound actions section above, the Target parameter to the AddToQueue Action is an activity. But since all activities inherit from the activitypointer EntityType, you must include the following property in the entity JSON to specify the type of entity is a letter: "@odata.type": "Microsoft.Dynamics.CRM.letter".
Two other examples are AddMembersTeam Action and RemoveMembersTeam Action because the Members parameter is a collection of systemuser EntityType which inherits it's ownerid primary key from the principal EntityType. If you pass the following JSON to represent a single systemuser in the collection, it is clear that the entity is a systemuser and not a team EntityType, which also inherits from the principal entitytype.
{ "Members": [{
"@odata.type": "Microsoft.Dynamics.CRM.systemuser",
"ownerid": "5dbf5efc-4507-e611-80de-5065f38a7b01"
}]
}
If you do not specify the type of entity in this situation, you can get the following error: "EdmEntityObject passed should have the key property value set.".
通过Web API调用Action时各种类型输入参数传递值的方法的更多相关文章
- 如何让ASP.NET Web API的Action方法在希望的Culture下执行
在今天编辑推荐的<Hello Web API系列教程--Web API与国际化>一文中,作者通过自定义的HttpMessageHandler的方式根据请求的Accep-Language报头 ...
- Only one complex type allowed as argument to a web api controller action.
错误内容: message":"An error has occurred.","exceptionMessage":"Only one c ...
- ABP开发框架前后端开发系列---(10)Web API调用类的简化处理
在较早期的随笔<ABP开发框架前后端开发系列---(5)Web API调用类在Winform项目中的使用>已经介绍了Web API调用类的封装处理,虽然这些调用类我们可以使用代码生成工具快 ...
- ABP开发框架前后端开发系列---(5)Web API调用类在Winform项目中的使用
在前面几篇随笔介绍了我对ABP框架的改造,包括对ABP总体的介绍,以及对各个业务分层的简化,Web API 客户端封装层的设计,使得我们基于ABP框架的整体方案越来越清晰化, 也越来越接近实际的项目开 ...
- ABP开发框架前后端开发系列---(4)Web API调用类的封装和使用
在前面随笔介绍ABP应用框架的项目组织情况,以及项目中领域层各个类代码组织,以及简化了ABP框架的各个层的内容,使得我们项目结构更加清晰.上篇随笔已经介绍了字典模块中应用服务层接口的实现情况,并且通过 ...
- 控制ASP.NET Web API 调用频率与限流
ASP.NET MVC 实现 https://github.com/stefanprodan/MvcThrottle ASP.NET WEBAPI 实现 https://github.com/stef ...
- Web API对application/json内容类型的CORS支持
假设有一简单架构分为前后两部分,其一是Angular构成的前端页面站点,另一个则是通过ASP.NET Web API搭建的后端服务站点.两个站点因为分别布署,所有会有CORS(Cross-Origin ...
- 搭建MVC及WEB API项目框架时碰到的问题集合
前言 刚开始创建MVC与Web API的混合项目时,碰到好多问题,今天拿出来跟大家一起分享下.有朋友私信我问项目的分层及文件夹结构在我的第一篇博客中没说清楚,那么接下来我就准备从这些文件怎么分文件夹说 ...
- ASP.NET Web API 通过参数控制返回类型(JSON|XML)
一个很实用的技巧,可以在访问web api服务的时候指定返回数据的格式类型,比如 json 或者 xml. 因为 web api 默认返回的是XML格式,但是现在json 比较流行,同时网上也有其他的 ...
随机推荐
- Android 混淆那些事儿
本文来自于腾讯Bugly公众号(weixinBugly),未经作者同意,请勿转载,原文地址:https://mp.weixin.qq.com/s/WmJyiA3fDNriw5qXuoA9MA 作者:l ...
- S-CMS企建v3二次SQL注入
S-CMS企建v3二次SQL注入 0x01 前言 继上一篇的S-CMS漏洞再来一波!首发T00ls 0x2 目录 Sql注入二次SQL注入 0x03 Sql注入 漏洞文件:\scms\bbs\bbs. ...
- Java面试题整理:这些Java程序员面试中经常遇见的题目,必须掌握才能有好结果
1.是否可以从一个static方法内部发出对非static方法的调用? 不可以.因为非static方法是要与对象关联在一起的,必须创建一个对象后,才可以在该对象上进行方法调用,而static方法调用时 ...
- ansible基础-Jinja2模版 | 测试
一 简介 注:本文demo使用ansible2.7稳定版 Jinja2的测试语句被用来评估一个条件表达式,并且最终返回True或False,经常和「when」语句搭配使用. 测试语句和过滤器的相同点: ...
- [SQL]LeetCode184. 部门工资最高的员工 | Department Highest Salary
The Employee table holds all employees. Every employee has an Id, a salary, and there is also a colu ...
- [Swift]LeetCode211. 添加与搜索单词 - 数据结构设计 | Add and Search Word - Data structure design
Design a data structure that supports the following two operations: void addWord(word) bool search(w ...
- Cassandra与Mongo的事务实现之分布式协议
摘要 NoSql不同于关系型数据库,是分布式存储,因此想要实现关系型数据库中的事务就不是那么简单了.本文结合Cassandra中的paxos和Mongo的two phase commit来谈谈Nosq ...
- 10.Flask上下文
1.1.local线程隔离对象 不用local对象的情况 from threading import Thread request = ' class MyThread(Thread): def ru ...
- HTTPS 到底加密了什么?
关于 HTTP 和 HTTPS 这个老生常谈的话题,我们之前已经写过很多文章了,比如这篇<从HTTP到HTTPS再到HSTS>,详细讲解了 HTTP 和 HTTPS 的进化之路,对的没错, ...
- C++版 - 剑指offer面试题38:数字在已排序数组中出现的次数
数字在已排序数组中出现的次数 提交网址: http://www.nowcoder.com/practice/70610bf967994b22bb1c26f9ae901fa2?tpId=13&t ...