使用JavaScript验证Microsoft Dynamics实体表单



我正在Microsoft Dynamics Customer Engagement中编写我的第一个JavaScript扩展,但我无法使脚本按我想要的方式运行。我正在尝试检查country字段是否为空。如果是,我想设置一个通知,并将其作为必填字段。如果它不是空的,并且国家是美国,我想检查州和邮政编码是否为空。如果是,我想设置一个通知,并使它们成为必填字段。如果国家不是美国,我想清除州和邮政编码的通知。如果国家是美国,州和邮政编码都有人,我想清除通知。

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

我有一个要在保存时执行的集合。请参阅下面的代码。我已经做了一段时间了,这让我很生气。感谢您的帮助。

如果现在看起来一团糟,我深表歉意。我已经更改了大约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.");
}
}

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

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

最新更新