Search code examples
javascriptdynamics-365powerapps-modeldriven

Dynamics 365 custom javascript won't execute save after setting field level notification


I set a field level notification when the data in the field is not valid. Then I change the data to something that is valid but because I have set the notification onSave will not be fired.

function ValidateFields(executionContext) {

console.log("Init");

var formContext = executionContext.getFormContext();

var bfenummer_value =  formContext.getAttribute("kk_ejerlav").getValue();
var bfenummer_control = formContext.getControl("kk_ejerlav");
var bfenummer_uniqueid = "bfenummer";

if(!isNumeric(bfenummer_value))
{
    bfenummer_control.setNotification("Angiv et tal mellem -2147483648 og 2147483647", bfenummer_uniqueid);
    console.log("Set " + isNumeric(bfenummer_value));

    // setTimeout(function () {
    //     bfenummer_control.clearNotification(bfenummer_uniqueid);            
    //     console.log("Clear " + isNumeric(bfenummer_value));
    // }, 1000);
}
else {
    bfenummer_control.clearNotification(bfenummer_uniqueid);            
    console.log("Clear " + isNumeric(bfenummer_value));
}

}

function isNumeric(value) {
  return /^-?\d+$/.test(value);
}

Solution

  • You need to run a function on OnChange event for the "kk_ejerlav" field.
    Something like this:

    function ejerlavChanged(executionContext){
        var formContext = executionContext.getFormContext();
    
        var bfenummer_value =  formContext.getAttribute("kk_ejerlav").getValue();
        var bfenummer_control = formContext.getControl("kk_ejerlav");
        var bfenummer_uniqueid = "bfenummer";
    
        if(isNumeric(bfenummer_value)){
            bfenummer_control.clearNotification(bfenummer_uniqueid);            
            console.log("Clear " + isNumeric(bfenummer_value));
        }
    }
    
    function isNumeric(value) {
      return /^-?\d+$/.test(value);
    }