admin管理员组文章数量:1349697
I have a requirement to call a Action Process from Javascript. My Action Accept 2 Input Parameters and 1 output Param. Below is the screenshot of my Action
I have a textField in my Form, and on it's onChange
event I'm calling this CallAction Method. Below is the 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));
}
When running this script I'm getting the following error in chrome console
POST .0/taqi_getPrice 404
Sometime it also says
Request header field Access-Control-Allow-Headers is not allowed by Access-Control-Allow-Headers
I have a requirement to call a Action Process from Javascript. My Action Accept 2 Input Parameters and 1 output Param. Below is the screenshot of my Action
I have a textField in my Form, and on it's onChange
event I'm calling this CallAction Method. Below is the 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));
}
When running this script I'm getting the following error in chrome console
POST https://techgulf.crm4.dynamics./api/data/v9.0/taqi_getPrice 404
Sometime it also says
Share Improve this question edited Apr 15, 2019 at 1:25 Arun Vinoth PrecogTechnologies 22.8k17 gold badges62 silver badges178 bronze badges asked Apr 14, 2019 at 18:59 Muhammad TaqiMuhammad Taqi 3152 gold badges6 silver badges20 bronze badges 1Request header field Access-Control-Allow-Headers is not allowed by Access-Control-Allow-Headers
- Modern good example: carldesouza./… – Don Cheadle Commented Aug 17, 2021 at 17:05
2 Answers
Reset to default 3Well I created Exact same Action as you mentioned in your screenshot, Except Entity I used is Account. I used below code to fire Action and it did worked for me without any issue and returned the value as expected.
May be for Testing you could provide static Guid and see how you get the result.
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));
Change the below line
req.open("POST", Xrm.Page.context.getClientUrl() + "/api/data/v9.0/taqi_getPrice", false);
like this one below:
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>>
.
Reference
Looks like you need a synchronous Action call execution (as you’re using false in req.open
) otherwise you can use Xrm.WebApi.online.execute
which is always Asynchronous. Read more
本文标签: How to call Actions from Javascript Microsoft DynamicsStack Overflow
版权声明:本文标题:How to call Actions from Javascript Microsoft Dynamics - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743849581a2549758.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论