Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics 365 | Integration, Dataverse...
Answered

How to Show the Field, only if users selects "Others" from the Multi choice column using JS script

Posted on by Microsoft Employee

I know that we cannot use business rule for mutli chocie column, so i created the js script for showing the field , only if users selects "Others" from the Multi choice column  with the help of this blog

Hide and Show Subgrid on basis of selected option/value using JavaScript in MS Dynamics 365 CRM. – Microsoft Dynamics 365 CRM (wordpress.com)

I have multi choice column with name "intendedproductusers" and one of the choice is "Others" with value - '100000005'

If user selects "Others" then i want to show the column "intendedproductusersother" or else hide this column

function Showhidecoulmn(executionContext) {
    var formContext = executionContext.getFormContext(); 
    var raiseRequest = formContext.getAttribute("intendedproductusers").getValue(); 
    if(raiseRequest == 100000005)
   {
           formContext.getControl('intendedproductusersother').setVisible(true);
   }
 
   else
   {    
formContext.getControl('intendedproductusersother').setVisible(false); 
   }
But when i used the above code and followed the same steps as in blog, but it was working, let me know if I'm missing something here?
}
  • Verified answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to Show the Field, only if users selects "Others" from the Multi choice column using JS script

    Hi psk_12,

    I have posted the new solution to the new thread!:)

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to Show the Field, only if users selects "Others" from the Multi choice column using JS script

    Hello   

    i was testing the form and everything is working fine used the same code you had given, but just had one issue when I select some option in multi choice column and then I remove it , I'm getting the below error, if I select again some options then it will work properly.

    when I open the new form then i will not receive any error, only when i select option and remove all options then i get this error, basically when i select something and then i remove it only if field is blank i get error, may something to do with this line not sure - if (selectedOptions.filter(i => i.value === 100000005).length > 0)

    7026.error1.jpg

    error message -Cannot read properties of null (reading 'filter')Session Id: e363e5ac-3692-4724-a98c-d9c5b10ff1b7Correlation Id: 7aa5f389-8056-485d-ac0a-cb1a033b5facEvent Name: onchangeFunction Name: ShowhidecoulmnenvWeb Resource Name: azmdc_HideIntendedEnvironmentOther.jsSolution Name: ActivePublisher Name: DefaultPublisherdevrccTime: Wed Nov 10 2021 11:09:52 GMT+0530 (India Standard Time)

    when i downloaded the error log- 

    TypeError: Cannot read properties of null (reading 'filter')
    at Showhidecoulmnenv (https://.crm4.dynamics.com/%7b637721179470000131%7d/webresources/azmdc_HideIntendedEnvironmentOther.js:8:25)
    at y._executeFunctionInternal (https://.crm4.dynamics.com/uclient/scripts/app.js?v=1.4.3368-2110.2:1980:5296)
    at y.execute (https://.crm4.dynamics.com/uclient/scripts/app.js?v=1.4.3368-2110.2:1980:3765)
    at https://.crm4.dynamics.com/uclient/scripts/app.js?v=1.4.3368-2110.2:147:26335
    at i (https://.crm4.dynamics.com/uclient/scripts/app.js?v=1.4.3368-2110.2:308:88)
    at ee._executeIndividualEvent (https://.crm4.dynamics.com/uclient/scripts/app.js?v=1.4.3368-2110.2:147:26309)
    at ee._executeEventHandler (https:/.crm4.dynamics.com/uclient/scripts/app.js?v=1.4.3368-2110.2:147:23314)
    at Object.execute (https://.crm4.dynamics.com/uclient/scripts/app.js?v=1.4.3368-2110.2:147:22742)
    at N._executeSyncAction (https://.crm4.dynamics.com/uclient/scripts/app.js?v=1.4.3368-2110.2:923:692)
    at N._executeSync (https://.crm4.dynamics.com/uclient/scripts/app.js?v=1.4.3368-2110.2:923:419)

    Error Details:
    Event Name: onchange
    Function Name: Showhidecoulmnenv
    Web Resource Name: azmdc_HideIntendedEnvironmentOther.js
    Solution Name: Active
    Publisher Name: DefaultPublisherdevrcc

    code used - same has you have given

    function Showhidecoulmnenv(executionContext) {
        var formContext = executionContext.getFormContext();
        var raiseRequest = formContext.getAttribute("azmdc_intendeduseenvironment");

        //Returns Array of Selected OptionSet Values, example [1001,1002,1005 ]
        var selectedOptions = raiseRequest.getSelectedOption();

        if (selectedOptions.filter(i => i.value === 100000003).length > 0) {
            formContext.getControl('azmdc_intendeduseenvironmentother').setVisible(true);
        }

        else {
            formContext.getControl('azmdc_intendeduseenvironmentother').setVisible(false);
        }
    }

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to Show the Field, only if users selects "Others" from the Multi choice column using JS script

    Hello

    Thank you so much for the help, it worked perfectly.

    This was the first time Im working on custom code and thought I would not be able achieve this req.

    But you have given the exact code, I didn't had to change anything this was really helpful.

    thanks again

  • Verified answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to Show the Field, only if users selects "Others" from the Multi choice column using JS script

    Hi psk_12,

    For multi chocie column, you can select multi choices at the same time.

    As long as the selected option contains "Others", the condition is met, you can refer following code(just change column name):

    function Showhidecoulmn(executionContext) {
        var formContext = executionContext.getFormContext();
        var raiseRequest = formContext.getAttribute("new_intendedproductusers");
    
        //Returns Array of Selected OptionSet Values
        var selectedOptions = raiseRequest.getSelectedOption();
    
        if (selectedOptions.filter(i => i.value === 100000005).length > 0) {
            formContext.getControl('new_intendedproductusersother').setVisible(true);
        }
    
        else {
            formContext.getControl('new_intendedproductusersother').setVisible(false);
        }
    }

    Test:

    (1)Fields:

    pastedimage1636357855014v3.pngpastedimage1636357911837v4.png

    (2)Add js resource to 'intendedproductusers' field onChange event

    pastedimage1636357996177v5.png

    Result:

    pastedimage1636357675169v2.png

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!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans