操作
操作是 Microsoft Dynamics 365 中的一种流程。您可以直接从工作流或对话框调用操作,包括自定义操作,而无需编写代码!更多信息:从工作流或对话框调用自定义操作
也可以通过运行使用 Microsoft Dynamics 365 Web 服务的自定义代码来调用操作。
您可以调用操作:
它可以从客户端和服务器端调用,启用单点方法(实施一次,随处使用),例如:- 从在插件、自定义工作流和任何 C# 代码中执行的代码。
来自放置在应用程序中并使用 JavaScript 代码执行操作的命令。
可以直接接收输入参数并返回输出参数,类似于组织级别的 Web 服务
通过与另一个使用 Microsoft Dynamics 365 Web 服务的系统集成。
从使用 Microsoft Dynamics 365 Web 服务的自定义客户端应用程序。
为什么要使用操作?
操作为组合业务逻辑提供了一系列可能性。在 Actions 之前,实现业务流程的主要方式仅限于插件或自定义工作流活动。使用操作,您可以执行操作,例如创建、更新、删除、分配或执行操作。在内部,操作会创建自定义 Dynamics 365 消息。通过操作,您可以创建自定义消息(例如:submitquote、leadtoax 等。定义并激活操作后,开发人员可以像使用 Microsoft Dynamics 365 平台提供的任何其他消息一样使用该消息。
假设您在报价单上有一个按钮,它将信息从 CRM 发送到另一个平台(例如另一个平台是 AX)。
创建并激活自定义操作(设置 > 流程)
现在您可以在某些事件(OnLoad、OnSave 等)上从 JavaScript 调用此 Action(ofs_submitquotetoax)。在此示例中,我从报价表单上的 SUBMIT QUOTE 按钮调用操作,该操作将报价信息发送到其他系统 (AX)。
// Call this below method from Button click event or from any event on the form
// For Alert.showLoding method you can see another Alert.js library
// For Process.callAction method you can see another Process.js library
// You can download Alert.js & Process.js from this Path: https://drive.google.com/drive/folders/0B2CUbevE8v9YMkZlMEhUZ3NJc1U
function submitquote() {
var actionName = "ofs_submitquoteax";
var entityName = Xrm.Page.data.entity.getEntityName();
var entityId = Xrm.Page.data.entity.getId();
Alert.showLoading("Submitting...", 400, 150);
var inputParams = [
{
key: "EntityRef", type: Process.Type.EntityReference,
value: new Process.EntityReference(entityName, entityId)
}
];
// call process callection method
Process.callAction(actionName, inputParams, cloneSuccessCallback, errorCallback);
}
function cloneSuccessCallback() {
Alert.hide();
Alert.show("Action Success", "", null, "SUCCESS");
Alert.hide();
var entityName = Xrm.Page.data.entity.getEntityName();
var entityId = Xrm.Page.data.entity.getId();
Xrm.Utility.openEntityForm(entityName, entityId);
//Xrm.Page.data.refresh();
}
function errorCallback(error, trace) {
alert(error);
alert(alert(error));
}
e,对于此事件,您可以注册和触发插件并接收输入参数(在我们的例子中,我们将输入参数键作为 EntityRef 发送,其中我们发送 entityName 和 entityId。
参数:操作唯一名称、输入参数(数组)、成功回调(函数)、错误回调(函数)、CRM 基本 URL(表单/视图不需要)
每个输入参数对象应包含键、值和类型。类型由 Process.Type 枚举定义。 EntityReference 值应该是一个包含 id 和 entityType 的对象。
成功回调函数应该接受一个参数,该参数是一组输出参数,每个参数都包含键和值。
您可以通过以下方式编写插件并访问输入参数
protected override void ExecuteCrmPlugin(LocalPluginContext localContext)
{ // Register the plugin in PreValidation stage as this plugin will trigger from Javascript (Action)
if (localContext == null)
{
throw new InvalidPluginExecutionException("localContext");
}
IPluginExecutionContext context = localContext.PluginExecutionContext;
if (context.Depth > 1) { return; }
IOrganizationService service = localContext.OrganizationService;
ITracingService trace = localContext.TracingService;
Entity quoteEntity = null;
EntityReference qEntity = null;
if (context.InputParameters.Contains("EntityRef") && context.InputParameters["EntityRef"] is EntityReference)
{
//if (context.PrimaryEntityName.ToLower() != "quote") { return; }
qEntity = context.InputParameters["EntityRef"] as EntityReference;
if (qEntity.LogicalName.ToLower().Equals("quote"))
{
try
{
quoteEntity = service.Retrieve("quote", qEntity.Id, new ColumnSet("ofs_parentaccountid", "quotenumber", "revisionnumber", "ofs_well"));
// Execute Your logic
}
catch (Exception ex)
{
trace.Trace(string.Format("Exception Quote_Create_AXIntegration Plugin: {0}", new[] { ex.ToString() }));
}
}
}
else { return; }
}
注册您的插件,然后按以下方式注册步骤,您可以在下面的屏幕截图“ofs_submitquoteax”中看到您的自定义消息名称;
参考:https://community.dynamics.com/crm/b/mylifemicrosoftdynamiccrm/archive/2017/04/17/microsoft-dynamics-crm-actions