Confirmation dialog while saving Dataverse record

There are times we might need to have a confirmation dialog before saving the record and in the case of cancel, needs to refresh the form data to previous state. Confirmation dialog is achievable using the client side SDK scripts. It is pretty easy to put the confirmation dialog in the save event, the tricky part is how to prevent saving and resetting the old data when confirm dialog is cancelled.

The below code help to make use of the save event functions to better use and achieve the need.

function onSave(executionContext) {
    var depth = sessionStorage.getItem("depth");
    if(depth == null || depth == "0"){
        console.log("Depth - "+ depth);
        sessionStorage.setItem("depth", "0");
        var formContext = executionContext.getFormContext();
        executionContext.getEventArgs().preventDefault();
        var tagMetaData = [];
        const tagNames = [];
        var xml = formContext.data.entity.getDataXml();
        var node = (new DOMParser()).parseFromString(xml, "text/xml").documentElement;
        var nodes = node.querySelectorAll("*");
        if(nodes.length > 0){
            for(let i = 0; i < nodes.length; i++) {
                if(nodes[i].getAttribute("type") == '1'){
                    tagMetaData.push({type:"1",name:nodes[i].tagName});
                    tagNames.push("_"+nodes[i].tagName+"_value");
                }else{
                    tagMetaData.push({type:"2",name:nodes[i].tagName});
                    tagNames.push(nodes[i].tagName);
                }            
            }
            var selectFields = tagNames.join();
            console.log(selectFields);
            
            var confirmStrings = { text:"Are you sure you want to save the record?", title:"Confirmation Dialog" };
            var confirmOptions = { height: 200, width: 450 };
            Xrm.Navigation.openConfirmDialog(confirmStrings, confirmOptions).then(
            function (success) {    
                if (success.confirmed){
                    sessionStorage.setItem("depth", "1");
                    formContext.data.save().then(function success(result){
                        sessionStorage.setItem("depth", "0");
                    },
                    function error(result){
                        sessionStorage.setItem("depth", "0");
                    });
                }
                else{
                    Xrm.WebApi.online.retrieveRecord(formContext.data.entity.getEntityName(), formContext.data.entity.getId(), "?$select="+ selectFields).then(
                        function success(result) {
                            for(let a = 0; a < tagMetaData.length; a++){
                                if(tagMetaData[a].type == "1"){
                                    var fieldFormattedValue = result["_"+tagMetaData[a].name+"_value@OData.Community.Display.V1.FormattedValue"];
                                    var fieldLogicalName = result["_"+tagMetaData[a].name+"_value@Microsoft.Dynamics.CRM.lookuplogicalname"];
                                    var fieldValue = result["_"+tagMetaData[a].name+"_value"];

                                    var lookupData = new Array();
                                    var lookupItem = new Object();
                                    lookupItem.id = fieldValue;
                                    lookupItem.name = fieldFormattedValue;
                                    lookupItem.entityType = fieldLogicalName;
                                    lookupData[0] = lookupItem;
                                    formContext.getAttribute(tagMetaData[a].name).setValue(lookupData);
                                }else{
                                    var fieldValue = result[tagMetaData[a].name];
                                    formContext.getAttribute(tagMetaData[a].name).setValue(fieldValue);
                                }
                            }
                            formContext.data.refresh(false);
                        },
                        function(error) {
                            Xrm.Utility.alertDialog(error.message);
                        }
                    );
                }
            });
        }
    }
}

Advertisement

2 thoughts on “Confirmation dialog while saving Dataverse record

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s