Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics 365 | Integration, Dataverse...
Suggested answer

This form can’t be saved due to a custom setting

(0) ShareShare
ReportReport
Posted on by

Hi everyone,

 I have an event on save which consists of an API call to check some status. Based on that status i am trying to save changes. So, for this, i used preventDefault to cancel the save operation which I did before the API call.

When i try to save the form again with "FormContext.data.save()", it throws "This form can’t be saved due to a custom setting". I also tried using preventDefault after the api call but the form was saved before hitting the response of the api.

Please find below my code:

// JavaScript source code

function onAccountFormLoad(executionContext) {
debugger;
var formContext = executionContext.getFormContext();
formContext.data.entity.addOnSave(
(() => {

return async (eContext) => {
eContext.getEventArgs().preventDefaultOnError();
let result = await validateSaveAsync(eContext);
}
}
)());
}
var validateSaveAsync = (eContext) => {
var formContext = eContext.getFormContext();
var agreement = formContext.getAttribute("msdyn_agreement").getValue();

return new Promise(function(resolve, reject) {
var boolRule1 = true;
if (agreement != null && agreement.length > 0) {
var id = agreement[0].id;
eContext.getEventArgs().preventDefault();
var result = Xrm.WebApi.retrieveRecord("msdyn_agreement", id, "?$select=msdyn_systemstatus").then(
function success(result) {
if (result["msdyn_systemstatus"] === 690970003) {
//Xrm.Navigation.openAlertDialog({text:"Current work order can't be saved because of Cancelled agreement"});
boolRule1 = false;
}else{
var formContextTest = eContext.getFormContext();
formContextTest.data.save().then(function() {
alert("Form saved successfully");
}, function(error) {
var errorMessage = error.message;
console.log(errorMessage);
});
}
resolve(boolRule1);
},
function(error) {
//Xrm.Navigation.openAlertDialog({ text: error.message });
reject(error.message);
}
);
} else {
resolve(boolRule1);
}
})
}

Please find below the screenshot error message:

Screenshot-_2800_154_2900_.pngScreenshot-_2800_153_2900_.png


What I am missing or doing wrong? Can please someone provide an example to solve the above scenario?

Any advice is highly appreciated.

Thank you!

  • Suggested answer
    Community Member Profile Picture
    on at
    RE: This form can’t be saved due to a custom setting

    Hi Suman Bhattaraj,

    This is because when you invoke formContextTest.data.save() method, the on save event would be also triggered. And you use preventDefault() method to prevent auto save which would cause the error.(This error will recur a lot of times.)

    To avoid this, please change your code like this:

    //this is variable that shows if validation was successfully passed or not
    var isValidationNeeded = true;
    function onAccountFormLoad(executionContext) {
    debugger;

    //When onAccountFormLoad(executionContext) method is invoked by formContextTest.data.save() method, reset isValidationNeeded variable. Your form data would be autosaved and use return to terminate the execution of this function to avoid it be prevent save by eContext.getEventArgs().preventDefaultOnError() method.
    if (!isValidationNeeded) {
    isValidationNeeded = true;
    return;
    }

    var formContext = executionContext.getFormContext();
    formContext.data.entity.addOnSave((() = >{

    return async(eContext) = >{
    eContext.getEventArgs().preventDefaultOnError();
    let result = await validateSaveAsync(eContext);
    }
    })());
    }
    var validateSaveAsync = (eContext) = >{
    var formContext = eContext.getFormContext();
    var agreement = formContext.getAttribute("msdyn_agreement").getValue();

    return new Promise(function(resolve, reject) {
    var boolRule1 = true;
    if (agreement != null && agreement.length > 0) {
    var id = agreement[0].id;
    eContext.getEventArgs().preventDefault();
    var result = Xrm.WebApi.retrieveRecord("msdyn_agreement", id, "?$select=msdyn_systemstatus").then(function success(result) {
    if (result["msdyn_systemstatus"] === 690970003) {
    //Xrm.Navigation.openAlertDialog({text:"Current work order can't be saved because of Cancelled agreement"});
    boolRule1 = false;
    } else {
    //validation is passed and change it to false
    isValidationNeeded = false;
    var formContextTest = eContext.getFormContext();
    formContextTest.data.save().then(function() {
    alert("Form saved successfully");
    },
    function(error) {
    var errorMessage = error.message;
    console.log(errorMessage);
    });
    }
    resolve(boolRule1);
    },
    function(error) {
    //Xrm.Navigation.openAlertDialog({ text: error.message });
    reject(error.message);
    });
    } else {
    resolve(boolRule1);
    }
    })
    }

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Ramesh Kumar – Community Spotlight

We are honored to recognize Ramesh Kumar as our July 2025 Community…

Congratulations to the June Top 10 Community Leaders!

These are the community rock stars!

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

Leaderboard > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

#1
Adis Profile Picture

Adis 136 Super User 2025 Season 1

#2
Sohail Ahmed Profile Picture

Sohail Ahmed 81

#3
Jonas "Jones" Melgaard Profile Picture

Jonas "Jones" Melgaard 77 Super User 2025 Season 1

Product updates

Dynamics 365 release plans