Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/410.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Dynamics 365 CRM插件-无法将托管解决方案中的两个选项字段设置为空_Javascript_Microsoft Dynamics_Onload_Dynamics Crm Online_Dynamics 365 - Fatal编程技术网

Javascript Dynamics 365 CRM插件-无法将托管解决方案中的两个选项字段设置为空

Javascript Dynamics 365 CRM插件-无法将托管解决方案中的两个选项字段设置为空,javascript,microsoft-dynamics,onload,dynamics-crm-online,dynamics-365,Javascript,Microsoft Dynamics,Onload,Dynamics Crm Online,Dynamics 365,我有两个环境:开发和测试。我希望实现如下所述的功能: if (fields.email.getValue() == null) { fields.emailAllowed.setValue(null); fields.emailAllowed.setRequiredLevel("required"); } else { fields.emailAllowed.setRequiredLevel("none"); } if (fiel

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

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

此JavaScript代码在开发环境中运行良好,但由于某些未知原因,它在测试环境中保存空值时

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

创建插件==>预验证
更新插件==>预验证

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

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");
}
// 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中,当我更新电子邮件字段时,它也会自动将真值设置为允许的电话字段,即使它没有更改,并且我在CRM插件中已将其外部设置为空。还有5个这样的领域也受到同样的影响

同样的更改在DEV环境中可以正常工作,但在测试环境中不行

注意:我已经交叉验证了DEV环境的所有更改都已移动到托管解决方案中,并且在测试环境中

更新1:

我今天尝试在测试环境中调试JavaScript更改,并通过它正确地更新了记录。但是当我关闭调试器并用新记录测试同一场景时,它再次更新了所有记录

更新2:

因为我的JavaScript函数也在onLoad事件上被调用,所以它可以设置NULL值,但看起来它无法找到控件或其他东西,这就是它在调试时工作但不是在常规模式下工作的原因

下面是在加载事件时执行的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的布尔字段。要么有一个全局或本地optionset(允许NULL)和两个值-这将允许以NULL值开始记录&无需每次都使用javascript或插件来处理数据

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

发现了问题

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

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

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


注意:我没有使用
OptionSet
而不是
TwoOptionSet
字段,这样客户就可以在其他内置功能中正确使用默认字段。

是的,我知道这一点。就连我也只选择了OptiStart field,但正如我的前辈所建议的,我必须尝试使用两个选项集。顺便说一句,仅供参考,在托管解决方案(开发环境)环境中,我能够存储空值。但在托管解决方案中,它的行为很奇怪。而且,在这个特殊的场景中,我需要确保如果用户拥有数据字段(例如电子邮件),那么在允许字段(允许电子邮件)中必须有值(不为null)。因此,如果我确实使用选项集,我需要设置额外的检查,以确保值不应为null。@PratikSoni我也很好奇为什么这在环境之间不一致&托管解决方案是其背后的唯一原因。。不幸的是,我有同样的问题。:)甚至我都很好奇,这可能是aTwo选项集的数据类型吗?我在这里还会错过其他的场景吗?我们正在尝试max使用solution more的默认字段-这是两个选项集-以便我们可以利用与这些字段相关的其他功能,而无需进行任何其他更改。