[英]How to mark an appointment as “completed” when the “finished” button is clicked using JavaScript in dynamics 365
My code below does not work, it changes to completed and quickly changes back. 我下面的代码不起作用,它更改为已完成,然后迅速改回。 ..................................................................................................................................................... .................................................. .................................................. .................................................
function OnLoad() {
Xrm.Page.data.process.addOnProcessStatusChange(statusOnChange);
}
function statusOnChange() {
status = Xrm.Page.data.process.getStatus();
if (status == "finished") {
markAsComplete();
}
}
function markAsComplete(){
if (Xrm.Page.getAttribute("statecode") != null && Xrm.Page.getAttribute("statuscode") != null){
Xrm.Page.getAttribute("statecode").setValue(1); //Changing Status to Completed
Xrm.Page.getAttribute("statecode").setSubmitMode("always");
Xrm.Page.getAttribute("statuscode").setValue(3); //Changing Status Reason to Completed
Xrm.Page.getAttribute("statuscode").setSubmitMode("always");
}else{
alert("statecode field is not available on the form");
}
}
To change the state of a record from JavaScript, you should either call a workflow or send a PATCH
request to the Web API . 要从JavaScript更改记录的状态,您应该调用工作流或向Web API发送PATCH
请求。
An update via the Web API would look as follows: 通过Web API进行的更新如下所示:
var appointmentId = Xrm.Page.data.entity.getId();
var entity = {};
entity.statuscode = 3;
entity.statecode = 1;
var req = new XMLHttpRequest();
req.open("PATCH", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/appointments(" + appointmentId + ")", true);
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 === 204) {
//Success - No Return Data - Do Something
}
else {
Xrm.Utility.alertDialog(this.statusText);
}
}
};
req.send(JSON.stringify(entity));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.