【发布时间】:2021-02-19 17:01:19
【问题描述】:
我正在 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.");
}
}
【问题讨论】:
-
要检查的几件事。假设你进入 UCI。 1) 您已将此脚本添加到表单中。 2)注册了表单的“onSave”方法 3)传递了注册onSave的执行上下文。 4) 您是否尝试调试并点击您的调试器语句?
-
你测试了吗?
标签: javascript validation dynamics-crm crm microsoft-dynamics