Skip to main content

JavaScript – Prevent Save - Model Driven App



function onSave(executionContext)

 {

    var formContext = executionContext.getFormContext();

    var errorId = "error";

    var eventArgs = executionContext.getEventArgs();

    //To see the Save Mode return value in your browser console

    console.log("Save Mode = " + eventArgs.getSaveMode());

    //If save mode is 'Save and Close' or 'Auto Save', inhibit default behavior i.e. save

    if (eventArgs.getSaveMode() == 2 || eventArgs.getSaveMode() == 70 || eventArgs.getSaveMode() == 1) {

        var productObj = formContext.getAttribute("product").getValue();

        var assetObj = formContext.getAttribute("customerasset").getValue();

        if (productObj == null && assetObj == null) {

            eventArgs.preventDefault();

            formContext.ui.setFormNotification("Product or Customer Asset must be provided", "ERROR", errorId);

        }

        else

        {

            formContext.ui.clearFormNotification(errorId);

        }

    }

    //To see if the auto save behavior was prevented in your browser console

    console.log("Default behaviour prevented: " + eventArgs.isDefaultPrevented());

  }





Comments