[英]Dynamics 365 CRM Plugin - Unable to set two option field NULL in managed solution
I have 2 environments: DEV & TEST.我有 2 个环境:开发和测试。 I want to achieve functionality as mentioned as below:
我想实现如下所述的功能:
if (fields.email.getValue() == null) {
fields.emailAllowed.setValue(null);
fields.emailAllowed.setRequiredLevel("required");
}
else {
fields.emailAllowed.setRequiredLevel("none");
}
if (fields.phone.getValue() == null) {
fields.phoneAllowed.setValue(null);
fields.phoneAllowed.setRequiredLevel("required");
}
else {
fields.phoneAllowed.setRequiredLevel("none");
}
This JavaScript code is working fine in DEV environment but due to some unknown reason it is NOT saving the NULL value in TEST environment.此 JavaScript 代码在 DEV 环境中运行良好,但由于某些未知原因,它没有在 TEST 环境中保存 NULL 值。
To support that, I have added a new CRM plugin in which I have created two handlers:为了支持这一点,我添加了一个新的 CRM 插件,我在其中创建了两个处理程序:
Create Plugin ==> PreValidation
创建插件 ==> 预验证
Update Plugin ==> PreValidation更新插件 ==> 预验证
In both of these plugins, I am using logic as below:在这两个插件中,我使用的逻辑如下:
// Gets PreImage & Target Image combined
public void Execute()
{
var combinedTarget = GetCombinedTarget();
if (combinedTarget == null)
{
// log & throw error
}
MapEmailField(combinedTarget);
MapPhoneField(combinedTarget);
}
private void MapEmailField(combinedTarget)
{
// if Email is NULL and EmailAllowed IS NULL
if(combinedTarget.Metadata.Attributes.Email != null && combinedTarget.Metadata.Attributes.EmailAllowed == null)
{
//log & throw error
}
else if(combinedTarget.Metadata.Attributes.Email == null)
{
combinedTarget.Metadata.Attributes.EmailAllowed = null; // To save NULL value into CRM field
}
}
// same method for phone field mapping
// save the record
Now in CRM UI, when I update email field, it automatically sets TRUE value to Phone Allowed field too, even though it is not changed and I have externally set it NULL in CRM plugin.现在在 CRM UI 中,当我更新 email 字段时,它也会自动将 TRUE 值设置为Phone Allowed字段,即使它没有更改并且我已经在 CRM 插件中外部设置了 NULL 。 And there are 5 more such fields are getting affected in same way.
还有 5 个这样的领域也以同样的方式受到影响。
The same changes are working fine in DEV environment but not in TEST environment.相同的更改在 DEV 环境中运行良好,但在 TEST 环境中却不行。
NOTE: I have cross verified that all changes of DEV environment are moved into managed solution and it is there in TEST environment.注意:我已经交叉验证了 DEV 环境的所有更改都已移至托管解决方案中,并且在 TEST 环境中。
UPDATE 1:
更新 1:
I tried today debugging the JavaScript change in TEST environment, and through that it updated the records correctly.
我今天尝试在TEST环境中调试 JavaScript 更改,并通过它正确更新了记录。 But when I closed the debugger and test the same scenario with new record, it updated all records again!!
但是当我关闭调试器并使用新记录测试相同的场景时,它再次更新了所有记录!
UPDATE 2:
更新 2:
As my JavaScript function is getting called on onLoad event too so that it can set NULL value, but looks like it is not able to find the control or something and that's the reason it is working while debugging but not in regular mode.
由于我的 JavaScript function 也在 onLoad 事件中被调用,因此它可以设置 NULL 值,但在常规模式下看起来它无法找到原因,但它是调试的原因。
Below is the Javascript function which gets executed onLoad event:下面是在 onLoad 事件中执行的 Javascript function:
function handleAllowedFields(dataField, allowedField) {
return function () {
if (dataField == null || allowedField == null) {
return;
}
if (dataField.getValue() == null) {
allowedField.setValue(null);
allowedField.setRequiredLevel("none");
allowedField.controls.forEach(function (c) {
c.setDisabled(true);
});
} else {
// If dataField is having a value, then contact preference fields
// can have an value as Allow OR DoNotAllow ONLY.
allowedField.controls.forEach(function (c) {
c.setDisabled(false);
});
allowedField.setRequiredLevel("required");
}
}
}
Any help that how can I make my function to wait till all controls gets loaded on the form in onLoad event?关于如何让我的 function 等到所有控件在 onLoad 事件中加载到表单上的任何帮助?
As you aware, CRM Two options is not good enough to store NULL or treat it as a NULL-able boolean field.如您所知,CRM 两个选项不足以存储 NULL 或将其视为可 NULL 的 boolean 字段。 Either have a global or local optionset (this allows NULL) with two values - this will allow to start the record with a NULL value & no need of javascript or plugin to massage the data every time.
具有两个值的全局或本地选项集(这允许 NULL) - 这将允许使用 NULL 值开始记录,并且不需要 javascript 或插件每次都按摩数据。
Anyway script will not trigger when data import happens or anything outside the platform like web api calls - so plugin is good choice.无论如何,当数据导入发生或平台外的任何东西(如 web api 调用)时,脚本不会触发 - 所以插件是不错的选择。
Found the issue.发现问题。
It's true that Managed solution was unable to save the NULL value for two option type
field.托管解决方案确实无法为
two option type
字段保存 NULL 值。 While it was able to do that in unmanaged solution.虽然它能够在非托管解决方案中做到这一点。
Alongside with that, I found that onLoad event was not executed after onSave event in JavaScript and this was the reason behind the issue.除此之外,我发现在 JavaScript 中的 onSave 事件之后没有执行 onLoad 事件,这就是问题背后的原因。
Hence, what I did as a solution is I added an onChange event to modifiedon
field on the form and it is working like butter.因此,我作为解决方案所做的是在表单上的
modifiedon
字段中添加了一个 onChange 事件,它就像黄油一样工作。
PS: I'm not using OptionSet
instead of TwoOptionSet
field is so that the default fields gets utilised properly by the customer for every other built in functionality. PS:我没有使用
OptionSet
而不是TwoOptionSet
字段,以便客户可以正确使用默认字段来处理其他所有内置功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.