Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics 365 | Integration, Dataverse...
Suggested answer

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

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
    Community Member Microsoft Employee 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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans