I have three yes/no fields on my form that are used to identify if an address group is the primary or not. I implemented JavaScript OnChange events on these fields to ensure that they are mutually exclusive and this part works. The problem I am having now is that I want to prevent the user from deselecting a "primary". (effectively preventing the user from having no primary address selected) So if a toggle is set to yes and the user clicks on it, I don't want the value of the toggle to change. Originally I thought in the on change event, I would just handle the situation by checking if the value of the toggle was "no" meaning that the on change event fired but the value of the toggle was no.
This works partially, it only works if the value of the toggle was previously saved on the database. It appears there is a delay.....even though the on change event fired, the code is getting the value from the database and not what the current state of the toggle is. If I wait a while, the database catches up and all is fine. It is like the code formContext.getAttribute("crmcs_address1primaryindicator").getValue(); is getting the value from the database and not from what is actually on the form.
Is there a way to get the value of the toggle on the form and not what is saved to the database?
Here is the code I am using...
function OnLoad(executionContext) {
var formContext = executionContext.getFormContext();
// Set OnChange methods for primary address indicators
formContext.getAttribute("crmcs_address1primaryindicator").addOnChange(HandleAddress1Primary);
formContext.getAttribute("crmcs_address2primaryindicator").addOnChange(HandleAddress2Primary);
formContext.getAttribute("crmcs_address3primaryindicator").addOnChange(HandleAddress3Primary);
}
////////////////////////////////////////
// Address Primary Indicator Handlers
////////////////////////////////////////
function HandleAddress1Primary(executionContext) {
var formContext = executionContext.getFormContext();
var isPrimary = formContext.getAttribute("crmcs_address1primaryindicator").getValue();
if (isPrimary) {
formContext.getAttribute("crmcs_primaryaddressnumber").setValue(1);
formContext.getAttribute("crmcs_address1primaryindicator").setValue(true);
formContext.getAttribute("crmcs_address2primaryindicator").setValue(false);
formContext.getAttribute("crmcs_address3primaryindicator").setValue(false);
} else {
// Don't allow user to deselect Primary flag, there must be at least one primary
formContext.getAttribute("crmcs_primaryaddressnumber").setValue(1);
formContext.getAttribute("crmcs_address1primaryindicator").setValue(true);
formContext.getAttribute("crmcs_address2primaryindicator").setValue(false);
formContext.getAttribute("crmcs_address3primaryindicator").setValue(false);
}
}