微软动态CRM专家罗勇 ,回复334或者20190509可方便获取本文,同时可以在第一间得到我发布的最新博文信息,follow me!
Action (操作)是流程的一种,可以在工作流中调用,可以使用JavaScript和C#代码方便的调用,用的好是个很不错的东西。
怎么创建Action请参考官方文档:Create your own actions ,或者我以前的博文也不错。
假设操作的名称为new_InvokeOrderApitoCreateReturnPartsAction,是一个绑定到实体new_returnreq的操作,包括一个类型string的输入参数OrderNumber,没有输出参数:
下面是JavaScript调用示例:
var params = {}; params.OrderNumber = "4318009670"; var req = new XMLHttpRequest(); var clientUrl = Xrm.Utility.getGlobalContext().getClientUrl(); req.open("POST", encodeURI(clientUrl + "/api/data/v9.1/new_returnreqs(e95e8829-c370-e911-a82b-000d3a365fc2)/Microsoft.Dynamics.CRM.new_InvokeOrderApitoCreateReturnPartsAction"), 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) { req.onreadystatechange = null; if (this.status == 200) { var alertStrings = { text: "调用操作成功!" }; var alertOptions = { height: 120, 160 }; Xrm.Navigation.openAlertDialog(alertStrings, alertOptions).then( function success() { }, function () { } ); } else { var error = JSON.parse(this.response).error; Xrm.Navigation.openErrorDialog({ message: error.message }).then( function () { }, function () { }); } } }; req.send(JSON.stringify(params));
如果你的Action有复杂的参数类型,可以参考我的博文:通过Web API调用Action时各种类型输入参数传递值的方法
下面是C#代码调用示例:
using Microsoft.Xrm.Sdk; using Microsoft.Xrm.Tooling.Connector; using System; namespace ConsoleApp { class Program { static void Main(string[] args) { CrmServiceClient crmSvc = new CrmServiceClient(@"AuthType=Office365;Url=https://luoyongdemo.api.crm.dynamics.com;UserName=luoyong@luoyong.me;Password=pppsssss"); if (!crmSvc.IsReady) { throw new Exception("Crm Service is not ready!" + crmSvc.LastCrmError + crmSvc.LastCrmException.Message); } OrganizationRequest orgReq = new OrganizationRequest("new_InvokeOrderApitoCreateReturnPartsAction"); orgReq["OrderNumber"] = "4318009670"; orgReq["Target"] = new EntityReference("new_returnreq", Guid.Parse("e95e8829-c370-e911-a82b-000d3a365fc2")); OrganizationResponse response = crmSvc.Execute(orgReq); Console.WriteLine("程序执行完成,按任意键退出..."); Console.ReadKey(); } } }
如果创建的是全局的Action,现在Dynamcis 365 Customer Engagement从 9.0版本开始有新的简便调用方法了,就是 invokeProcessAction (Client API reference) 。
虽然官方文档没有明说仅仅支持全局Action,从我测试结果来看是仅仅支持全局Action,或许也有可能我不知道怎么调用绑定Action吧。
var params = {}; params.EntityLogicalName = "new_b2borderreturnreqdetails"; params.EntityId = "c87f8979-d172-e911-a833-000d3a375590"; Xrm.Utility.invokeProcessAction("new_DeleteDoNotNeedReturnPartsRecordsAction", params).then( function () { var alertStrings = { text: "调用全局操作成功!" }; var alertOptions = { height: 120, 160 }; Xrm.Navigation.openAlertDialog(alertStrings, alertOptions).then( function success(results) { }, function () { } ); }, function (errorobj) { console.log(errorobj); Xrm.Navigation.openErrorDialog({ message: "调用全局操作失败!" + errorobj.message }).then( function () { }, function () { }); });
若是上面例子调用全局action报错,比如:This action is not supported ,那就用旧的方法去调用全局Action吧,示例如下:
var params = {}; params.EntityLogicalName = "new_demo"; params.EntityId = "c87f8979-d172-e911-a833-000d3a375590"; var req = new XMLHttpRequest(); var clientUrl = Xrm.Utility.getGlobalContext().getClientUrl(); req.open("POST", encodeURI(clientUrl + "/api/data/v9.1/new_demoAction"), 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) { req.onreadystatechange = null; if (this.status == 204) { var alertStrings = { text: "调用操作成功!" }; var alertOptions = { height: 120, 160 }; Xrm.Navigation.openAlertDialog(alertStrings, alertOptions).then( function success() { }, function () { } ); } else { var error = JSON.parse(this.response).error; Xrm.Navigation.openErrorDialog({ message: error.message }).then( function () { }, function () { }); } } }; req.send(JSON.stringify(params));