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,...
Answered

Show/Hide Fields using Javascript and Naming Conventions: Event Handler/Error

(0) ShareShare
ReportReport
Posted on by 5

Hi There,

I'm working on a form that has fields that need to be hidden/shown based on another field selection. I used a naming convention for all the hidden/shown fields (begin with acpe_1A., acpe_1B, etc..). Instead of painstakingly putting each of these (and there are quite a few!) into a business rule, I'm guessing js could do this much more efficiently.

However, when I configure the event handler and web resource and attempt to use the form, I get an error that seems to indicate that the function in the web resource can't be found. 

pastedimage1683550870883v1.png

Here's the code (admittedly I'm non-experienced with this!):

function showFieldsBasedOnSelection(executionContext) {
  var formContext = executionContext.getFormContext();
  var triggerField = formContext.getAttribute("acpe_cpelevel");
  var selectedValue = triggerField ? triggerField.getValue() : null;
  
  var controls = formContext.ui.controls.get();

  for (var i in controls) {
    var control = controls[i];
    var controlName = control.getName();
    
    if (selectedValue === "Level 1A" && controlName.startsWith("acpe_1A")) {
      control.setVisible(true);
    } else if (selectedValue === "Level 1B" && controlName.startsWith("acpe_1B")) {
      control.setVisible(true);
    } else if (selectedValue === "Level 2A" && controlName.startsWith("acpe_2A")) {
      control.setVisible(true);
    } else if (selectedValue === "Level 2B" && controlName.startsWith("acpe_2B")) {
      control.setVisible(true);
    } else if (controlName.startsWith("acpe_1A") || controlName.startsWith("acpe_1B") || controlName.startsWith("acpe_2A") || controlName.startsWith("acpe_2B")) {
      control.setVisible(false);
    }
  }
}

I'm sure I've goofed somewhere... any ideas from the hive mind?

I have the same question (0)
  • Suggested answer
    XM-22040801-0 Profile Picture
    11 on at

    Hi,

    Remove the () when you register the function. You only need to define the name of the function.

    See:

    pastedimage1683551981687v1.png

    This should solve your problem, however I see another problem if the "acpe_cpelevel" field is an optionset : selectedValue will not contain a text but the numeric value.

    You will find below an adaptation using an association array instead of if else.

    function showFieldsBasedOnSelection(
        /** @type { Xrm.Events.EventContext } */
        executionContext) {
        const formContext = executionContext.getFormContext();
        const levelAttr = formContext.getAttribute("acpe_cpelevel");
    
        if (!levelAttr) {
            Xrm.Navigation.openAlertDialog({ text: "acpe_cpelevel attribute not found" });
            return;
        }
    
        const levelValue = levelAttr.getValue();
        const levelAssociations = [
            // Set the level code of the option set.
            { levelCode: 1, prefix: "acpe_1A" },
            { levelCode: 2, prefix: "acpe_1B"},
            { levelCode: 3, prefix: "acpe_2A" },
        ];
    
        levelAssociations.forEach(({ levelCode, prefix }) => {
            const controls = formContext.ui.controls.get(ctrl => ctrl.getName().startsWith(prefix));
            const visible = levelCode === levelValue;
            controls.forEach(ctrl => ctrl.setVisible(visible));
        });
      }

  • StuartH Profile Picture
    5 on at

    First problem, solved! Thank you!

    Now, it seems that when using this code, choosing any value in the pick-list causes all of the fields to show, not just the intended ones by (acpe_1A... acpe_1B...) prefix. And when returning the value to null, nothing disappears.

    So, I wondered if using the schema name ("acpe_CPELevel") as the getAttribute parameter would solve the problem ("acpe_cpelevel" is the logical name), but this throws the "...attribute not found" error you (helpfully!) included in the code.

    Any further suggestions?

  • Suggested answer
    XM-22040801-0 Profile Picture
    11 on at

    JS use the logical name.

    I think that in levelAssociations the prefix must be acpe_1a instead of acpe_1A (for example). Try to set the prefixes in lower case.

    Also, you probably need to register this function on form load.

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 108 Super User 2025 Season 2

#2
Jimmy Passeti Profile Picture

Jimmy Passeti 50 Most Valuable Professional

#3
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 49 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans