Hi
I have a model driven app where I want to check a client ID-number against the records already in the Dataverse table. When a match is found, the save-event has to be cancelled and a warning has to be displayed to the end-user.
I'm using the onSave event handler where I call the preventDefault() method, after which I look for existing records (with Xrm.WebApi.retrieveMultipleRecords) with an ID identical to the one provided in the new record.
function retrieveRecordsWithSameRRNR(rrnr, formContext, id) {
Xrm.WebApi.retrieveMultipleRecords("vtt_client", "?$select=vtt_clientid,vtt_naam&$filter=vtt_rrnr eq '"+rrnr+"'").then(
(success) => {
console.log(success);
if((success.entities.length > 0 && !id) || (success.entities.length > 1 && id)) {
//some notification warning the user that a client with the same ID already exists
} else {
console.log("rrnr ok");
formContext.data.save().then(
(success) => {
console.log(success);
},
(error) => {
console.log(error);
}
)
};
},
(error) => {
//error notification
}
)
};
function onSave(executionContext) {
var formContext = executionContext.getFormContext();
var rrnr = formContext.getAttribute("vtt_rrnr");
var rrnrClean = rrnr.getValue().toString().replace(/\D/g, "");
var id = formContext.data.entity.getId();
executionContext.getEventArgs().preventDefault();
retrieveRecordsWithSameRRNR(rrnrClean, formContext, id);
}
So far so good, but the trouble begins when no match is found and the save event can continue. Here I use the formContext.data.save() method in the callback of the retrieveMultipleRecords method, but when I try to create a new record in the app the data.save() method keeps returning an error.
Furthermore the save event keeps on firing indefinitely (as if it notices the error and tries to repeat saving only to reproduce the same error). The error-code is 2200000005 and the translated error message reads something like "the form cannot be saved due to a changed setting". It doesn't specify however what setting seems to be causing the problem (I have activated the asynchrone event handlers in the app-settings, but when I de-activate them, the error remains).
Instead of calling the data.save() method in the callback I've also tried using async/await, but the result is the same. I've looked around for solutions to the problem but haven't been able to find an answer. I could retrieve the records onLoad and check onSave whether the ID is in the retrieved records, but this isn't ideal.
Hopefully someone has encountered the same error and knows a solution!
Thanks in advance!


Report
All responses (
Answers (