locked
Using javascript want to make attachement mandatory and executionobj.getEventArgs().preventDefault() not working on " Save " RRS feed

  • Question

  • Hi,

    I wrote a javascript to make attachement mandatory in Notes and I have tried to restrict Save Operation using executionobj.geteventargs().preventdefault() but it is not working. please help regarding this !!

    Thanks & regards,

    Rajitha.
    Tuesday, March 15, 2016 5:22 AM

All replies

  • Hi Rajitha,

    You could use some JavaScript like this to check if a file was attached via a note - it allows the initial save because you can attach anything to a record that isn't yet created - then checks on subsequent saves. You would just need to incorporate the logic to determine the circumstances under which that check should be made.

    function OnSave(exectionObj) {
        if (Xrm.Page.data.entity.getId() === null) return;
    
        var serverUrl = Xrm.Page.context.getClientUrl();
        var oDataSelect = serverUrl + "/XRMServices/2011/OrganizationData.svc/AnnotationSet?$filter=ObjectId/Id eq guid'" + Xrm.Page.data.entity.getId() + "'&$select=IsDocument";
        var allowSave = false;
        var req = new XMLHttpRequest();
        req.open("GET", oDataSelect, false);
        req.setRequestHeader("Accept", "application/json");
        req.setRequestHeader("Content-Type", "application/json;charset=utf-8");
        req.onreadystatechange = function () {
            if (req.readyState === 4) {
                if (req.status === 200) {
                    var retrieved = JSON.parse(req.responseText).d;
                    for (var i = 0; i < retrieved.results.length; i++) {
                        if (retrieved.results[0].IsDocument) {
                            allowSave = true;
                            break;
                        }
                    }
                }
            }
        };
        req.send();
    
        if (!allowSave) {
            alert("Attach a file via a note");
            exectionObj.getEventArgs().preventDefault();
        }
    }
    


    Alessandro Graps

    Tuesday, March 15, 2016 11:38 AM
  • Hi Alessandro Graps,

    I Tried the code you provided it is preventing From Save Operation but in Notes it showing like "No records found" before and after save Operation !!! Please, Help Me.

    Thanks & regards,

    Rajitha


    Tuesday, March 15, 2016 12:58 PM
  • Hi Rajitha,

    I don't understand the problem. because you have no records found because you don't save the note:

    exectionObj.getEventArgs().preventDefault();

    and because there is not an attachment.



    Alessandro Graps

    Wednesday, March 16, 2016 7:45 AM