Dynamics 365-四:Action操作

参考文档

语义说明

核心名词

FetchXML:是基于Xml的查询语言,可以把它简单理解成SQL语句,通过它可以查询Dynamics 365 CRM的数据。开发人员可以在WebAPI或者Organization Service执行FetchXML查询来获取数据,类似于SqlHelper.QueryTable(sql)

Action:Dynamics 365 流程中的一种,允许开发人员进行自定义开发,用来定制并组合各种业务逻辑,比如商机赢单、订单提交。我们可以把它简单理解成C#中的一个方法,有输入参数、输出参数。操作的注册模式分为两种:一个是全局,一个是绑定到实体,可以获取到实体ID

Web API:是客户端连接服务端的一种方式,拥有良好的平台兼容性,不管什么平台都可以调用,与开发语言无关。它是基于OData v4.0实现,提供了各种现代化的Restful Web服务

Organization Service:是客户端连接服务端的另外一种方式,它是基于WCF技术实现,数据传输采用XML,仅使用于.NET客户端

Action

​ 操纵类似与我们常用的方法,用于扩展系统的标准功能,用来实现项目中的业务逻辑。操作可以针对单个实体,也可以是全局(也就是任意实体都可使用)全局方法/局部方法

​ 工作流中可以调用操作,JS也可以调用操作,通过后端C#代码也可调用操作。始终在组织范围内执行操作,不支持执行限制到用户、业务部门或组织的范围。

​ 和Plugin类似,都需要签名,注册到CRM中

自定义操作

新建Action流程

打开D365,进入我的流程中心

在流程中心页面,点击左上角新建按钮,输入流程名称(全英文),类别选择操作(Action),实体选择无(全局)/实体(局部),建议全局,点击确定创建

可选:定义输入参数及输出参数,完成后点击保存,发布,激活

新建Action项目

打开VS,新建类库项目(.NET 版本与D365版本对应)

新建类,继承IPlugin接口,并实现Execute方法

using Microsoft.Xrm.Sdk;
using System;

namespace ActionApp
{
    public class ConeAction : IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService service = serviceFactory.CreateOrganizationService(null); // 权限
            
            Random rd = new Random();

            string name = context.InputParameters["name"].ToString();
            string area = context.InputParameters["area"].ToString();
            string phone = context.InputParameters["phone"].ToString();

            Entity entity = new Entity("new_customer");
            entity["new_name"] = name + rd.Next(1000, 9999);
            entity["new_area"] = area + rd.Next(1000, 9999);
            entity["new_phone"] = phone + rd.Next(1000, 9999);
            //entity["new_contact"] = entityRef;
            entity["new_lasttime"] = DateTime.UtcNow.AddDays(rd.Next(1,100));

            service.Create(entity);

            context.OutputParameters["msg"] = "ok";
        }
    }
}

签名:右键项目,属性,签名,勾选为程序集签名,新建,填写文件名称,取消使用密码保护密钥文件,确定

编译:右键项目,清理,重新生成

注册/绑定Action

打开注册工具,登录,新建Assembly

在步骤一中选择Action项目编译的DLL文件

在步骤二中勾选添加(使用)的Action(一个类库可有多个action),点击选择添加

新建执行时机:找的上一步添加的类库集合,点击展开,右键添加的Action,选择添加Step

Message:与action流程名一致;Primary Entity:关联实体,若为实体则此Action为局部(实体),none为全局;其次,执行时期为PostOperation,完成

使用Action

JS调用

// 调用全局带参方法
function createEntity() {
	var entity = new Object();
	entity['name'] = '21586625'
	entity['area'] = '21586625'
	entity['phone'] = '21586625'
    
	var req = new XMLHttpRequest();
	req.open('post', Xrm.Page.context.getClientUrl() + "/api/data/v9.0/new_wyg_cone", false)
	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) {
			if (this.status == 200) alert(1)
		}
	}
	req.send(JSON.stringify(entity));
}

调试Action

与插件(Plugin)方式一致,详情

接收参数

字符串类型

string name = context.InputParameters["name"].ToString();
string area = context.InputParameters["area"].ToString();
string phone = context.InputParameters["phone"].ToString();

整数类型

string name = context.InputParameters["name"].ToString();
int money = (int)context.InputParameters["money"];
int age = (int)context.InputParameters["age"];

布尔类型

string name = context.InputParameters["name"].ToString();
bool gender = (bool)context.InputParameters["gender"];
int age = (int)context.InputParameters["age"];

日期类型

string name = context.InputParameters["name"].ToString();
bool gender = (bool)context.InputParameters["gender"];
int age = (int)context.InputParameters["age"];
DateTime time = (DateTime)context.InputParameters["time"];

选择列表

Entity(实体)类型

// Entity类型的参数建议指定@data.type (可以不指定,若是lookup到具体实体,不是customer,partylist类型),格式为Microsoft.Dynamics.CRM.实体逻辑名称
// 需要指定记录主键值,还有需要用到的该记录的其他字段值
var cus = {}
cus['@data.type'] = 'Microsoft.Dynamics.CRM.new_customer'
cus.new_customerid = '1377FF4A-B494-E811-8ACD-005056948ABE'
cus.new_name = 'libai'
cus.new_area = '上海市'
entity['cus'] = cus
Entity cus = (Entity)context.InputParameters["cus"];

EntityReference(查找)类型

// EntityReference类型的参数指定记录ID就可以,在流程参数类型中已执行实体
var cus = {}
cus.new_customerid = '56C134ED-A433-EB11-B392-005056993F73'
entity['cus'] = cus 
EntityReference cus = (EntityReference)context.InputParameters["cus"];
// or
var s = context.InputParameters["cus"];

EntityCollection(实体列表)类型

var cusitem = {}
cus['@data.type'] = 'Microsoft.Dynamics.CRM.new_customer'
cus.new_customerid = '1377FF4A-B494-E811-8ACD-005056948ABE'
cus.new_name = 'libai'
cus.new_area = '上海市'
entity['cus'] = [cusitem]
EntityCollection cus = (EntityCollection)context.InputParameters["cus"];
// or
var s = context.InputParameters["cus"];

返回参数

context.OutputParameters["msg"] = "ok";
posted @ 2020-12-01 15:49  位永光  阅读(2401)  评论(0编辑  收藏  举报