通过Web API调用Action时各种类型输入参数传递值的方法

时间:2021-01-19 15:04:43

本人微信公众号:微软动态CRM专家罗勇 ,回复280或者20180906可方便获取本文,同时可以在第一间得到我发布的最新博文信息,follow me!我的网站是 www.luoyong.me 。

Dynamics 365的Web API功能很强大也越来越强大,流程的一种操作(Action)的功能也很强大也越来越强大(有个特征很好用,就是这些步骤默认组成一个事物,要么都成功,要么都失败)。

工作流中可以调用操作,通过Web API也可以方便的调用。

最近操作的参数增加了Entity这种类型,调用的时候如何赋值呢?我下面这个操作有两个复杂点的输入参数,一个类型为EntityReference的输入参数,一个类型为Entity的输入参数,还有一个类型为EntityReference的输出参数。

通过Web API调用Action时各种类型输入参数传递值的方法

为啥要用到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.

JSON
{
 "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.".