[英]How can I prevent a form from saving in Dynamics 365
I want to prevent a form from saving if the entered "dni" value is already registered by another "person", I tried to keep the form from saving in its onSave event, and then if my condition matched, I'd make the form save then.如果输入的“dni”值已被另一个“人”注册,我想阻止表单保存,我试图阻止表单保存在其 onSave 事件中,然后如果我的条件匹配,我会制作表单然后保存。 So I made the following code:所以我做了以下代码:
function saveDni(executionContext) {
try {
const formContext = executionContext.getFormContext();
const currentDni = formContext.getAttribute("cr6ff_dni").getValue();
executionContext.getEventArgs().preventDefault();
var fetchXml = [
"<fetch top='1'>",
" <entity name= 'cr6ff_person'>",
" <filter>",
" <condition attribute='cr6ff_dni' operator='eq' value='", currentDni, "'/>",
" </filter>",
" </entity>",
"</fetch>",
].join("");
fetchXml = "?fetchXml=" + encodeURIComponent(fetchXml);
Xrm.WebApi.retrieveMultipleRecords("cr6ff_person", fetchXml).then(
(result) => {
if (result.entities.length == 0) {
formContext.data.entity.save();
}
}
)
}
catch(error) {
console.log("An error has occurred while validating the user's data.");
}
The thing is, when the save() method triggers, it also triggers this function back again, so I'm stuck in a loop.问题是,当 save() 方法触发时,它也会再次触发这个 function,所以我陷入了一个循环。 Any ideas as to how I could work this out?关于我如何解决这个问题的任何想法?
Let me recommend you the best practices.让我向您推荐最佳实践。
Xrm.WebApi.retrieveMultipleRecords
in form save for better User experience.避免像Xrm.WebApi.retrieveMultipleRecords
这样的异步调用形式保存以获得更好的用户体验。 Read more 阅读更多formContext.data.entity.save()
is deprecated, use formContext.data.save()
instead. formContext.data.entity.save()
已弃用,请改用formContext.data.save()
。 Reference 参考
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.