Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Microsoft Dynamics 365 | Integration, Dataverse...
Suggested answer

D365 CE Javascript setVisible not working in UCI

(0) ShareShare
ReportReport
Posted on by 185

Hi All,

I have created a simple JavaScript function that shows/hides a field based on an optionset value. It works fine until it is being set as displayed for the first time, once it is displayed(visible), then unable to not display the field, even though I am setting the setVisible to false.

I have debugged the code, and all the values are passing fine, only the issue is the department is being displayed (while it is supposed to be hidden).

Here is the code:

function onload(executionContext) {
    var formcontext = executionContext.getFormContext();
    window.setTimeout(function () {
        showhidefield(executionContext, formcontext);
    }, 5000);
}

function showhidefield(executionContext, formcontext) {
    debugger;
    if (formcontext != null) {       
    }
    else var formcontext = executionContext.getFormContext();
    //var category = Xrm.Page.getAttribute("sf_category").getValue();
    var category = formcontext.getAttribute("sf_category").getValue();
    if (category!= "undefined" && category == 979730002) {
        toggledisplay(formcontext, "sf_department", true);
       
    }
    else {
        toggledisplay(formcontext, "sf_department", false);       
       
    }
}
function toggledisplay(formcontext, fieldname, bool) {
    debugger;
    var save = true;
    if (bool) {
        //Xrm.Page.getControl(fieldname).setVisible(bool);
        //Xrm.Page.data.refresh(save);
        formcontext.getAttribute("sf_department").setValue(null);
        formcontext.getControl(fieldname).setVisible(bool);
        formcontext.data.refresh(save);
    }
}
  • LeoAlt Profile Picture
    16,331 Moderator on at
    RE: D365 CE Javascript setVisible not working in UCI

    Hi Nadiu,

    I'm so glad to hear that you've resolved the issue.

    Thanks for your sharing :-)

    Have a nice day!

    Best Regards,

    Leo

  • Suggested answer
    Srinivas Naidu Profile Picture
    185 on at
    RE: D365 CE Javascript setVisible not working in UCI

    Issue is fixed after removing the if (bool) condition in the last part of the code.

  • Srinivas Naidu Profile Picture
    185 on at
    RE: D365 CE Javascript setVisible not working in UCI

    Hi Leo,

    I have triggered in onchange and given the screenshots.

    I fixed the problem.

    It was a small error in the code, the if (bool) part, which was not required. I removed that piece and it is working as expected.

    Thanks for the inputs.

  • LeoAlt Profile Picture
    16,331 Moderator on at
    RE: D365 CE Javascript setVisible not working in UCI

    Hi partner,

    I saw that you added the method in onload event which means this event will only be triggered once.

    You should add this event to the option set field onChange event and remove the setTimeOut method and try again.

    Regards,

    Leo

  • Srinivas Naidu Profile Picture
    185 on at
    RE: D365 CE Javascript setVisible not working in UCI

    Default: visibility is set to not visible. And when value chosen in category, working as expected.

    screenshot-1PNG.png

    Similarly, when the required option is chosen, the Department field is set to visible.

    screenshot-2PNG.png

    But again when the previous or any other option in which the department is supposed to be hidden, the field remains visible.

    The issue is, script is triggered, values are passed, but only that the function is not working as expected. No errors.

    screenshot-3PNG.png

  • Srinivas Naidu Profile Picture
    185 on at
    RE: D365 CE Javascript setVisible not working in UCI

    Hi Leo,

    Thanks, I have already done the same.

    Made it invisible by default and enabling it only when required. But if user selected the option that makes it visible, the script is working fine as expected, then again if another value is selected in which it should be hidden, the script is not hiding the field. I will update with screenshots so that it is clear.

    Any suggestions welcome.

  • Suggested answer
    LeoAlt Profile Picture
    16,331 Moderator on at
    RE: D365 CE Javascript setVisible not working in UCI

    Hi partner,

    Since you want set a field visible only when we select a special value in option set, you should set the field unvisible in default.

    pastedimage1581335897335v1.png

    If so, when the option does not meet the requirement(equals 9797300002 in your case), the field will not display on the form.

    BTW, you could simplify your code like the following sample.

    function showField(executionContext){
        var formContext=executionContext.getFormContext();
        var option=formContext.getAttribute("new_option").getValue();
        if(option!=null&&option==1){
            formContext.getControl("new_showhide").setVisible(true);
        }
        else{
            formContext.getControl("new_showhide").setVisible(false);
        }
    }

    I tired in my instance and it worked well.

    pastedimage1581337456146v2.png

    pastedimage1581337472797v3.png

    In additional, you could also use business rule to do this without developing.

    Best Regards,

    Leo

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

🌸 Community Spring Festival 2025 Challenge 🌸

WIN Power Platform Community Conference 2025 tickets!

Jonas ”Jones” Melgaard – Community Spotlight

We are honored to recognize Jonas "Jones" Melgaard as our April 2025…

Kudos to the March Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 294,217 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 232,978 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,158 Moderator

Leaderboard

Product updates

Dynamics 365 release plans