下面的代码不起作用,它会更改为已完成并快速更改。 .................................................. .................................................. ................................................. < / p>
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");
}
}
答案 0 :(得分:2)
要从JavaScript更改记录的状态,您应该调用工作流或向Web API发送PATCH
请求。
通过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));