我有2个环境:DEV&TEST。我想实现如下所述的功能:
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插件,在其中创建了两个处理程序:
创建插件==>预先验证更新插件==>预先验证
在这两个插件中,我使用的逻辑如下:
// 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插件中具有外部设置为NULL,它也会自动将TRUE值设置为允许电话字段。而且还有5个此类字段以相同的方式受到影响。
相同的更改在DEV环境中可以正常工作,但在TEST环境中不能正常工作。
注意:我已经交叉验证了DEV环境的所有更改都已移入托管解决方案,并且它在TEST环境中。
您知道,CRM两个选项不足以存储NULL或将其视为可为NULL的布尔字段。具有两个值的全局或局部选项集(这允许NULL)-这将允许从NULL值开始记录,而无需使用javascript或插件每次都对数据进行处理。
[无论何时执行数据导入或平台之外的任何内容(例如Web api调用),任何脚本都不会触发-因此插件是不错的选择。