Skip to main content

Notifications

Customer experience | Sales, Customer Insights,...
Answered

Javascript error

Posted on by 10

There will be a multi-select field with four values.

When the value 'Other' is selected, another field which will be a text field called 'Other', will be visible on the same form.

When 'Other' is deselected or never chosen, the 'Other' text field will be hidden from the form.

JScript will be on an OnChange event which will be used when the multi-select field is changed.

The multi-select field name is sh_funderreportingschedule. 


The 'Other' text field name is sh_other


The multi-select field cannot have a default value,


Both fields are on the form.


I will use JavaScript in a web resource by copying and pasting the code into the text editor. 


function toggleOtherField() {
// Get the multiselect field and 'Other' text field elements
var multiSelectField = Xrm.Page.getControl("sh_funderreportingschedule");
var otherField = Xrm.Page.getControl("sh_other");

// Get the selected options in the multiselect field
var selectedOptions = multiSelectField.getAttribute().getValue();

// Check if 'Other' is among the selected options
var hasOtherOption = false;
selectedOptions.forEach(function(option) {
if (option.name === 'Other') {
hasOtherOption = true;
return;
}
});

// Show/hide the 'Other' text field based on whether 'Other' is selected in the multiselect field
otherField.setVisible(hasOtherOption);
}

// Register the OnChange event for the multiselect field
Xrm.Page.getAttribute("sh_funderreportingschedule").addOnChange(toggleOtherField);

Error- Cannot read properties of null (reading 'forEach')Session Id: 907bb68e-fbf7-4f58-8c1a-7ec1c550f18cCorrelation Id: 4a4e9032-a312-40f3-b9e7-6d966d4a6c4dEvent Name: onchangeTime: Mon Jan 09 2023 16:02:52 GMT+0000 (Greenwich Mean Time)

 

  • 4891s Profile Picture
    4891s 10 on at
    RE: Javascript error

    Than you for your patience Guido. It is now working perfectly, and I really appreciate the time. Have a great day and I owe you!

  • Verified answer
    Guido Preite Profile Picture
    Guido Preite 54,081 Super User 2024 Season 1 on at
    RE: Javascript error

    the value must be without commas, you wrote

    881,990,003

    but the value is

    881990003

    also you can do a console.log of the variable multiSelectField to make sure is an array of numbers

  • 4891s Profile Picture
    4891s 10 on at
    RE: Javascript error

    Also you were correct about adding it to Onload, thank you.

  • 4891s Profile Picture
    4891s 10 on at
    RE: Javascript error

    Hi Guido,

    Thanks for coming back to me again.

    The issue is pointing to this: if (multiSelectField != null && multiSelectField.includes(881,990,003)) {

    If I simply remove the part (&& multiSelectField.includes(881,990,003)) then it works fine. It must be an issue with the multiselect value. I have tried a 4, I have tried 881,990,003, I have tried the label "other" and none of them work. It just skips the line and goes to the else logic and keeps the field hidden.

      function showHideOtherField(executionContext) {

       formContext = executionContext.getFormContext();

       var multiSelectField = formContext.getAttribute("sh_funderreportingschedule").getValue();

       formContext.getControl("sh_other").setVisible(true);

       formContext.getAttribute("sh_other").setRequiredLevel("none");

    if (multiSelectField != null && multiSelectField.includes(881,990,003)) {

           formContext.getControl("sh_other").setVisible(true);

           formContext.getAttribute("sh_other").setRequiredLevel("required");

       }

    else {

           formContext.getControl("sh_other").setVisible(false);

           formContext.getAttribute("sh_other").setRequiredLevel("none");

       }

    }

  • Guido Preite Profile Picture
    Guido Preite 54,081 Super User 2024 Season 1 on at
    RE: Javascript error

    you should put the code that hides the field and set required to none inside the ELSE part of your IF, because is a multiselect, if you are going to select another value but keeping 4 as selected, it will first hide and after show again, if you put inside the ELSE part then it will hide only if the condition is not met.

    The script should also run inside the onload event of the form, because when you open the form you want to show the correct status, ideally the Other text field is added inside the form as hidden (if you put visible and after the script hides you can see it happens)

    hope it helps

  • 4891s Profile Picture
    4891s 10 on at
    RE: Javascript error

    Thanks Guido,

    I have altered the JavaScript to this:

    function showHideOtherField(executionContext)

    {

     // get the form context

     formContext = executionContext.getFormContext();

     // get the multiselect field and 'Other' text field

     var multiSelectField = formContext.getAttribute ("sh_funderreportingschedule").getValue();

    formContext.getControl("sh_other").setVisible(false);

    formContext.getAttribute("sh_other").setRequiredLevel("none")

    if(multiSelectField != null && multiSelectField.includes(4))

    {

     formContext.getControl("sh_other").setVisible(true);

    formContext.getAttribute("sh_other").setRequiredLevel("required")

    }

    }

    I am no longer getting JavaScript errors but:

    The text field sometimes appears and sometimes disappears. It is not consistent and seems buggy in behaviour. It is also not showing as required and for some reason the multi select value defaults to other on load of a new record and shows the other text field too.

    It should simply load and not default to anything.

    If the user clicks 'other' in the multi select then the other field should show and be mandatory and if they untick it then it should hide.

    The behaviour is set to run OnChange of the Multiselect field.

    Thanks for any further help you can provide.

  • Guido Preite Profile Picture
    Guido Preite 54,081 Super User 2024 Season 1 on at
    RE: Javascript error

    in your code you are checking by the name but getValue returns just the integer values.

    Or you check by the value of the option "Other" or your use the getSelectedOption so you get the selected objects (type option) array.

    also your code is using Xrm.Page (should be migrated to formContext), you are binding the onchange inside the code (I assume your script is running inside the OnLoad event?) and you don't actually need the getControl for the choices field, you can directly do the getAttribute.

    Bottom line, make sure how you want to check the value and after debug your code because it can be fixed/improved in different points.

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,240 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,149 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans