简体   繁体   中英

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 .

An update via the Web API would look as follows:

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));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM