如何从 Javascript Microsoft Dynamics 调用操作
How to call Actions from Javascript Microsoft Dynamics
我需要从 Javascript 调用操作流程。
我的操作接受 2 个输入参数和 1 个输出参数。下面是我的Action
的截图
我的表单中有一个 textField,在它的 onChange
事件上我调用了这个 CallAction 方法。下面是 JavaScript
function CallAction() {
var actionName = "taqi_getPrice";
var actionParameters = {
"base": "USD",
"TotalPrice": "200"
};
var actionResponse = activateCustomAction(actionName, actionParameters);
}
function activateCustomAction(actionName, actionParams) {
var req = new XMLHttpRequest();
req.open("POST", Xrm.Page.context.getClientUrl() + "/api/data/v9.0/taqi_getPrice", false);
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.onreadystatechange = function () {
if (this.readyState === 4) {
req.onreadystatechange = null;
if (this.status === 200) {
var results = JSON.parse(this.response);
alert('Success');
} else {
alert('fail');
//Xrm.Utility.alertDialog(this.statusText);
console.log(this);
}
}
};
req.send(JSON.stringify(actionParams));
}
当 运行 这个脚本时,我在 chrome 控制台中收到以下错误
POST https://techgulf.crm4.dynamics.com/api/data/v9.0/taqi_getPrice 404
有时它也说
Request header field Access-Control-Allow-Headers is not allowed by Access-Control-Allow-Headers
更改下面的行
req.open("POST", Xrm.Page.context.getClientUrl() + "/api/data/v9.0/taqi_getPrice", false);
喜欢下面这个:
req.open("POST", Xrm.Page.context.getClientUrl() + "/api/data/v9.0/taqi_cars(" + Id + ")/Microsoft.Dynamics.CRM.taqi_getPrice", false);
Basically we need to pass name of the Entity Set with id of the record
followed by name of the action appended with Microsoft.Dynamics.CRM.
In case of global action, we just need the
Microsoft.Dynamics.CRM.<<ActionName>>
.
看起来你需要一个同步的 Action 调用执行(因为你在 req.open
中使用 false)否则你可以使用 Xrm.WebApi.online.execute
这总是异步。 Read more
好吧,我创建了与您在屏幕截图中提到的完全相同的操作,除了我使用的实体是帐户。我使用下面的代码触发 Action,它确实对我有用,没有任何问题,并按预期返回了值。
可能是为了测试,您可以提供静态 Guid 并查看如何获得结果。
var parameters = {};
parameters.base = "123";
parameters.TotalPrice = "222";
var req = new XMLHttpRequest();
req.open("POST", Xrm.Page.context.getClientUrl() + "/api/data/v9.1/accounts(DC86C293-CA4F-E911-A82F-000D3A385A1C)/Microsoft.Dynamics.CRM.crmp_TestAction2", false);
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.onreadystatechange = function() {
if (this.readyState === 4) {
req.onreadystatechange = null;
if (this.status === 200) {
var results = JSON.parse(this.response);
} else {
Xrm.Utility.alertDialog(this.statusText);
}
}
};
req.send(JSON.stringify(parameters));
我需要从 Javascript 调用操作流程。 我的操作接受 2 个输入参数和 1 个输出参数。下面是我的Action
的截图onChange
事件上我调用了这个 CallAction 方法。下面是 JavaScript
function CallAction() {
var actionName = "taqi_getPrice";
var actionParameters = {
"base": "USD",
"TotalPrice": "200"
};
var actionResponse = activateCustomAction(actionName, actionParameters);
}
function activateCustomAction(actionName, actionParams) {
var req = new XMLHttpRequest();
req.open("POST", Xrm.Page.context.getClientUrl() + "/api/data/v9.0/taqi_getPrice", false);
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.onreadystatechange = function () {
if (this.readyState === 4) {
req.onreadystatechange = null;
if (this.status === 200) {
var results = JSON.parse(this.response);
alert('Success');
} else {
alert('fail');
//Xrm.Utility.alertDialog(this.statusText);
console.log(this);
}
}
};
req.send(JSON.stringify(actionParams));
}
当 运行 这个脚本时,我在 chrome 控制台中收到以下错误
POST https://techgulf.crm4.dynamics.com/api/data/v9.0/taqi_getPrice 404
有时它也说
Request header field Access-Control-Allow-Headers is not allowed by Access-Control-Allow-Headers
更改下面的行
req.open("POST", Xrm.Page.context.getClientUrl() + "/api/data/v9.0/taqi_getPrice", false);
喜欢下面这个:
req.open("POST", Xrm.Page.context.getClientUrl() + "/api/data/v9.0/taqi_cars(" + Id + ")/Microsoft.Dynamics.CRM.taqi_getPrice", false);
Basically we need to pass name of the Entity Set with id of the record followed by name of the action appended with Microsoft.Dynamics.CRM. In case of global action, we just need the
Microsoft.Dynamics.CRM.<<ActionName>>
.
看起来你需要一个同步的 Action 调用执行(因为你在 req.open
中使用 false)否则你可以使用 Xrm.WebApi.online.execute
这总是异步。 Read more
好吧,我创建了与您在屏幕截图中提到的完全相同的操作,除了我使用的实体是帐户。我使用下面的代码触发 Action,它确实对我有用,没有任何问题,并按预期返回了值。
可能是为了测试,您可以提供静态 Guid 并查看如何获得结果。
var parameters = {};
parameters.base = "123";
parameters.TotalPrice = "222";
var req = new XMLHttpRequest();
req.open("POST", Xrm.Page.context.getClientUrl() + "/api/data/v9.1/accounts(DC86C293-CA4F-E911-A82F-000D3A385A1C)/Microsoft.Dynamics.CRM.crmp_TestAction2", false);
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.onreadystatechange = function() {
if (this.readyState === 4) {
req.onreadystatechange = null;
if (this.status === 200) {
var results = JSON.parse(this.response);
} else {
Xrm.Utility.alertDialog(this.statusText);
}
}
};
req.send(JSON.stringify(parameters));