Reputation: 11
I am writing my first JavaScript extension in Microsoft Dynamics Customer Engagement, and I can't get my script to work the way I want it. I am trying to check if the country field is empty. If it is, I want to set a notification and make it a required field. If it is not empty and the country is United States, I want to check if state and zip are empty. If they are, I want to set a notification and make them required fields. If the country is not United States, I want to clear the notifications for state and zip. And if the country is United States and state and zip are populated, I want to clear the notifications.
The country field uses a lookup, and I am accounting for that by assigning the country lookup value for the conditional check on state and zip.
I have this set to execute on save. Please see my code below. I have been at this for a while, and it's driving me mad. Any help is appreciated.
And I apologize if this looks like a hot mess right now. I've changed the code about 872 times and finally threw my hands in the air.
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.");
}
}
Upvotes: 1
Views: 1452
Reputation: 22846
The important snippet to make the fields mandatory is missing, refer the sample changes below and modify your code accordingly.
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");
}
Upvotes: 1