我正在尝试使用正则表达式查询动态 crm 帐户数据,以将帐户名称作为重复检测措施进行比较,以通过确认对话框向最终用户发出警告,用户可以在需要时继续创建重复的帐户名称。在这里,在查询中,我试图过滤帐户名称,忽略名称中的任何空格,然后将其与正在创建/更新的帐户名称进行比较,然后在字符串匹配时填充警告。我打算使用 js,因为 OOB 重复检测似乎只匹配完整的单词,插件会限制用户完全创建重复记录。
请提出实现相同目标的方法
我试过下面的代码导致错误(不支持“包含”功能。),我也试过Microsoft.Dynamics.CRM.Contains,这也导致错误
function checkForDuplicateAccountNames(executionContext) {
// Use the Xrm.WebApi object to query Dynamics 365 for accounts with the same name
var formContext = executionContext.getFormContext();
var accountName = formContext.getAttribute("name").getValue().replace(/\s+/g, '');
var query = "?$select=name&$filter=contains(replace(name, ' ', ''),'" + accountName + "') and statecode eq 0";
executionContext.getEventArgs().preventDefault();
Xrm.WebApi.retrieveMultipleRecords("account", query).then(
function success(result) {
if (result.entities.length > 0) {
// If there are duplicate account names, show a confirm dialog to the user
var confirmStrings = { text: "An account with this name already exists. Do you still want to create/update it?" };
var confirmOptions = { height: 200, width: 450 };
Xrm.Navigation.openConfirmDialog(confirmStrings, confirmOptions).then(
function (success) {
// If the user clicks Yes, allow the form to save
if (success.confirmed) {
isValidationNeeded = false;
formContext.data.entity.save();
}
},
function (error) {
console.log(error.message);
}
);
}
else {
isValidationNeeded = false;
formContext.data.entity.save();
}
},
function error(error) {
console.log(error.message);
}
);
}