首页 > 解决方案 > 使用 JavaScript 的 Microsoft Dynamics 实体表单验证

问题描述

我正在 Microsoft Dynamics Customer Engagement 中编写我的第一个 JavaScript 扩展,但我无法让我的脚本按照我想要的方式工作。我正在尝试检查国家/地区字段是否为空。如果是,我想设置一个通知并将其设为必填字段。如果它不是空的并且国家是美国,我想检查 state 和 zip 是否为空。如果是,我想设置一个通知并将它们设为必填字段。如果国家不是美国,我想清除州和邮编的通知。如果国家是美国并且州和邮编都已填充,我想清除通知。

country 字段使用查找,我通过为 state 和 zip 的条件检查分配国家查找值来解释这一点。

我有这个设置在保存时执行。请在下面查看我的代码。我已经有一段时间了,这让我发疯。任何帮助表示赞赏。

如果现在这看起来像一团糟,我深表歉意。我已经更改了大约 872 次代码,最后把手举到了空中。

function SetMandatoryFields(executionContext) {

    debugger;
    var formContext = executionContext.getFormContext();
    var addressState, addressCountry, addressZip, countryLookup;

    //Initialize variables to corresponding form fields. Get value of form fields and set fields to required if applicable.
    addressState = formContext.getAttribute("usf_address1stateid").getValue();
    addressZip = formContext.getAttribute("address1_postalcode").getValue();
    addressCountry = formContext.getAttribute("usf_address1countryid").getValue();

    //If country is United States, set state as required field if it is null or blank, and prompt user for field value. Else, clear prompt.
    if (addressCountry != null && addressCountry != "") {
        formContext.getControl("usf_address1countryid").clearNotification();
        countryLookup = formContext.getAttribute("usf_address1countryid").getValue()[0].name;
        //If country lookup is equal to United States, make state and zip required fields.
        if (countryLookup == "United States") {
            if (addressState == null || addressState == "") {
                formContext.getControl("usf_address1stateid").setNotification("State is a required field.");
            }
            else {
                formContext.getControl("usf_address1stateid").clearNotification();
            }
            if (addressZip == null || addressZip == "") {
                formContext.getControl("address1_postalcode").setNotification("Zip/Postal Code is a required field.");
            }
            else {
                formContext.getControl("address1_postalcode").clearNotification();
            }
        }
        //If country lookup is not United States, remove state and zip requirement
        else if (countryLookup != "United States") {
            
            formContext.getControl("address1_postalcode").clearNotification();
            formContext.getControl("usf_address1stateid").clearNotification();
        }
    }
    else {
        formContext.getControl("usf_address1countryid").setNotification("Country is a required field.");
    }
}

标签: javascriptvalidationdynamics-crmcrmmicrosoft-dynamics

解决方案


缺少使字段成为必填项的重要片段,请参阅下面的示例更改并相应地修改您的代码。

if (addressState == null || addressState == "") {
    formContext.getControl("usf_address1stateid").setNotification("State is a required field.");
    formContext.getAttribute("usf_address1stateid").setRequiredLevel("required");
}
else {
    formContext.getControl("usf_address1stateid").clearNotification();
    formContext.getAttribute("usf_address1stateid").setRequiredLevel("none");
}

推荐阅读