Dynamics 365 CRM 插件 - 无法在托管解决方案中设置两个选项字段 NULL

Dynamics 365 CRM Plugin - Unable to set two option field NULL in managed solution

我有 2 个环境:开发和测试。我想实现如下所述的功能:

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");
}

此 JavaScript 代码在 DEV 环境中运行良好,但由于某些未知原因,它 NOT 在 TEST 环境中保存 NULL 值。

为了支持这一点,我添加了一个新的 CRM 插件,我在其中创建了两个处理程序:

Create Plugin ==> PreValidation
Update Plugin ==> PreValidation

在这两个插件中,我使用的逻辑如下:

// 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

现在在 CRM UI 中,当我更新电子邮件字段时,它也会自动将 TRUE 值设置为 Phone Allowed 字段,即使它不是更改了,我在 CRM 插件中外部将其设置为 NULL。还有 5 个这样的字段以同样的方式受到影响。

相同的更改在 DEV 环境中运行良好,但在 TEST 环境中运行不正常。

注意:我已经交叉验证 DEV 环境的所有更改都已移入托管解决方案,并且它在 TEST 环境中。

UPDATE 1:

I tried today debugging the JavaScript change in TEST environment, and through that it updated the records correctly. But when I closed the debugger and test the same scenario with new record, it updated all records again!!

更新 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 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");
        }
    }
}

关于如何让我的函数等待所有控件在 onLoad 事件中加载到表单上的任何帮助?

如您所知,CRM 两个选项不足以存储 NULL 或将其视为 NULL-able 布尔字段。有一个带有两个值的全局或本地选项集(这允许 NULL)——这将允许以 NULL 值开始记录并且不需要 javascript 或插件每次都处理数据。

无论如何,当发生数据导入或平台外的任何事情(如网络 api 调用时,脚本不会触发 - 因此插件是不错的选择。

找到问题。

托管解决方案确实无法保存 two option type 字段的 NULL 值。虽然它能够在非托管解决方案中做到这一点。

除此之外,我发现 JavaScript 中的 onSave 事件后没有执行 onLoad 事件,这就是问题背后的原因。

因此,作为解决方案,我所做的是在表单的 modifiedon 字段中添加了一个 onChange 事件,它就像黄油一样工作。

P.S.: 我没有使用 OptionSet 而不是 TwoOptionSet 字段,这样客户就可以将默认字段正确地用于所有其他内置功能。