Update record when checkbox checked using JavaScript and save d365

On checkbox on change following function called

--------------------------------------------------------------------


function IsClientDisease(executionContext) {

    debugger;

    var formContext = executionContext.getFormContext();

    var selectedValues = formContext.getAttribute("ecl_isclientdisease").getValue();

    var id = formContext.data.entity.getId().replace("{", "").replace("}", "");

    if (selectedValues == 1) {

        var fetchbooking = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'>" +

            "  <entity name='contact'>" +

            "    <attribute name='fullname' />" +

            "    <attribute name='telephone1' />" +

            "    <attribute name='contactid' />" +

            "    <order attribute='fullname' descending='false' />" +

            "    <filter type='and'>" +

            "      <condition attribute='contactid' operator='eq' uitype='contact' value='" + id + "' />" +

            "    </filter>" +

            "    <link-entity name='msdyn_workorder' from='ecl_client' to='contactid' link-type='inner' alias='ae'>" +

            "      <filter type='and'>" +

            "        <filter type='or'>" +

            "          <condition attribute='msdyn_systemstatus' operator='ne' value='690970003' />" +

            "          <condition attribute='msdyn_systemstatus' operator='ne' value='690970000' />" +

            "        </filter>" +

            "      </filter>" +

            "    </link-entity>" +

            "  </entity>" +

            "</fetch>";

        var fetchXML = "?fetchXml=" + encodeURIComponent(fetchbooking);

        var evrVar = Xrm.WebApi.retrieveMultipleRecords('contact', fetchXML).then(function success(evrVarRec) {

            debugger;

            if (evrVarRec != null && evrVarRec.entities != null && evrVarRec.entities.length > 0) {

                var alertStrings = { confirmButtonLabel: "Ok", text: "In system found workorders which system status not unsheduled and completed. Please first update the status completed after try again!", title: "Alert" };

                var alertOptions = { height: 120, width: 260 };

                Xrm.Navigation.openAlertDialog(alertStrings, alertOptions).then(

                    function (success) {

                        formContext.getAttribute("ecl_isclientdisease").setValue(false);

                    },

                    function (error) {

                        console.log(error.message);

                    }

                );

            }

            else {

                DeactivateContact(id, formContext);

            }

        });

    }


    function DeactivateContact(id, formContext) {

        debugger;

        //var data = {};

        ////entity["contactid"] = id;

        //data["statecode"] = 1;

        //data["statuscode"] = 2;

        //data["ecl_isclientdisease"] = true;

        var data = {

            "statecode": "1",

            "statuscode": "2" ,// inactive

            "ecl_isclientdisease": "true"

        };

        Xrm.WebApi.updateRecord("contact", id, data).then(function success(result) {

            debugger;

            console.log("status updated");

            autosaveWhenFieldIsDirty(formContext);

          

        }, function (error) {

            debugger;

            console.log(error.message);

            // handle error conditions

        });

    }


    function autosaveWhenFieldIsDirty(formContext) {

        var isFieldDirty = formContext.getAttribute("ecl_isclientdisease").getIsDirty();

        var saveOptions = {

            saveMode: 70

        };

        if (isFieldDirty == true) {

            formContext.data.save(saveOptions).then(

                function (success) {

                    console.log(success);

                },

                function (error) {

                    console.log(error);

                }

            );

        }

    }

}

Comments

Popular posts from this blog

Power Automate compose vs variable

Power pages custom button add in gried