CRM Dynamics 如何检查是否存在空选项列表值

问题描述 投票:0回答:1

我在 CRM Dynamics 中有一个名为“确认”的选项列表字段,具有以下值

  • 是的
  • 没有
  • 进行中

还有一个默认/标准空值,允许我们重置选项列表选择。

如何使用 JavaScript 检查默认空值是否存在?目前,使用以下代码时,添加了两个默认选项。如何确保只显示一个?

  • ---选择----
  • ---选择---
  • 是的
  • 没有
  • 进行中
function addBlankOptionToPicklistIfNotExists(formContext) {
    var targetAttributes = [
        "test_confirmpicklist"
    ];

    targetAttributes.forEach(function(targetAttribute) {
    
    var control = formContext.getControl(targetAttribute);

    formContext.getControl(targetAttribute).removeOption(-1);

        if (control) {
            // check if the attribute value is null or undefined
            var attribute = control.getAttribute();
            var attributeValue = attribute.getValue();

            if (attributeValue != null && attributeValue != undefined) {
                // check if the picklist already contains a blank option
                var options = attribute.getOptions();
                var nullOptionExists = options.some(option => option.value == -1);

                // If blank option doesn't exist, add it
                if (!nullOptionExists) {
                    control.addOption({ value: -1, text: "--Select--" }, 0);
                }
            }
        }
    });
}
javascript dynamics-crm
1个回答
0
投票

---Select--- 选项不是真正的选项,它只是标识字段的

null
值的文本/占位符。当属性要求级别为“非”业务必需时,此选项可供用户使用。在这种情况下,用户应该可以选择清除输入字段。 您可以使用

formContext.getAttribute(targetAttributeName).getRequiredLevel()

进行检查。 (请参阅

getRequiredLevel(客户端 API 参考)| MS Learn
)。 当字段为“业务必填”时,只要尚未选择任何值,表单仅显示 ---Select--- 提示(此处不是选项)。一旦选择了某个选项,用户就无法清除该字段,因为需要一个值。

但是,可以使用 JavaScript 清除默认值或撤消用户的选择。只需将字段的值设置为

null

:

formContext.getAttribute(targetAttributeName).setValue(null);

© www.soinside.com 2019 - 2024. All rights reserved.