web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Suggested Answer

Field OnChange JavaScript not getting most current value of toggle

(0) ShareShare
ReportReport
Posted on by 5

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

I have the same question (0)
  • Suggested answer
    meelamri Profile Picture
    13,216 User Group Leader on at

    Hi,

    formContext.getAttribute().getValue() does not retrieve the value from the database. The problem is certainly elsewhere.

    How is the value of the crmcs_address1primaryindicator field changed? Is this a manual action or automated by another script?

  • meelamri Profile Picture
    13,216 User Group Leader on at

    Hi,

    Any news ?

  • Jeremy Schoneveld Profile Picture
    5 on at

    The value is changed by the user clicking the toggle on the page.  I have further discovered that the problem is that I cannot change the value of the field within the fired onChange event.  I wanted to just simply prevent the user from changing a primary flag to no by clicking on the same toggle that was set to yes.  For further clarification, we have three toggles that indicate which of the three is primary.  I want them to change it to no only by clicking one of the other two thus making it so that at least one is indicating primary.  I don't want the situation where all three toggles can be set to no.

  • Suggested answer
    meelamri Profile Picture
    13,216 User Group Leader on at

    If I understand your need, you can simply put the field in read-only mode if it contains the value YES. This allows us to not change the value

  • Jeremy Schoneveld Profile Picture
    5 on at

    Is there a method that I can use to accomplish that in javascript?

    I have tried:

    formContext.getAttribute("crmcs_address1primaryindicator").setDisabled(true);

  • Suggested answer
    meelamri Profile Picture
    13,216 User Group Leader on at

    Hi,

    Yes you can use formContext.getControl(arg).setDisabled(true). Did it not work for you?

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 170 Super User 2025 Season 2

#2
#ManoVerse Profile Picture

#ManoVerse 70

#3
Jimmy Passeti Profile Picture

Jimmy Passeti 50 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans