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

Hiding field in BPF until another field is filled in

(0) ShareShare
ReportReport
Posted on by 240

Hello, 

So I have a BPF with a couple fields in the first stage of BPF which I want to hide when the form is opened. They should remain hidden until a field in first stage of BPF is filled in. Based on what is entered, one of the hidden fields will become visible and then further options would be chosen for the field which has become visible.

My problem is hiding the fields when the form loads. I used a quick script to hide them which works. <<formContext.getControl('header_process_fieldname').setVisible(false);>>

Next, I use business rules to display the hidden fields based on what is chosen. That works too. But when the hidden field is displayed and filled in, and I refresh the page - the hidden field which was just filled in disappears again (I am guessing the script kicks in again). 

I also deactivated the business rule and reactivated it after putting the script in place (keeping in mind the latest business rule operates first). 

How do I go about this? Is there any issue with the ordering?

Thank you

I have the same question (0)
  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Hello,

    Recommendation - don't use combination of Business Rules and JS. Choose one. In your case it will be JS so remove all of your BR and write JS that will do whatever is needed.

  • Aneeqa Pervaiz Profile Picture
    240 on at

    Hey,

    Thanks for your reply. How would the script look like to display a specific field based on what was entered. For example:

    There are four fields: A, B, C, and D.

    A is an option set with values to choose from: B, C, D are the options.

    So when a value is chosen as B - The B field should display which would be filled in by a lookup. Similarly, if C is chosen as the option in A, then C would be displayed and so on.

    Would it be different codes? I have one code loaded which keeps the B, C, D fields hidden when form is loaded. Where would the other code be stored?

    Would appreciate the help.

    Thank you.

  • Suggested answer
    TNS Profile Picture
    1,197 on at

    Hi,

    As , you told by some step your one hidden field will be visible let's take it "A" and whatever the options you choose in field "A" will be show/hide your B,C,D field.

    So you can do it by js or business rule which you like.

    Firstly for your A,B,C,D field go to their field properties from the form and ser their default visibility to false.By this your fields at form load will be not visible.

    Now if you use js,

    then put if/else condition for your A field for which condition it should be visible. and on basis of your A field only show visisbility of B,C and D field . This js you have to trigger at form on load.

    and another js you have to write on change trigger of A field which will show/hide your B,C and D field.

    Regards,

    TNS

  • Aneeqa Pervaiz Profile Picture
    240 on at

    Hey,

    It would solve my problem if I can default the B,C,D fields to be hidden when the form loads. I cannot use OOB 'Visible by default' to hide them - since these fields are in the BPF and not on the form. So I ran a script to hide them.

    Now I want a specific one to be visible when the field A is filled out (A is an option set with B,C,D as options). I used Business Rule to show either B,C,D depending on what was chosen in A. The problematic part is that when the option is chosen, the respective field becomes visible, but disappears again after I refresh the page. I think the script kicks in again and hides the B or C or D - whichever was chosen.

    This is what i need help with.

    If i use JS instead of BR, how would that code look like?

  • Suggested answer
    TNS Profile Picture
    1,197 on at

    Hi,

    You can try below code trigger it on load:

    function script(context)

    {

     var formContext = context.getFormContext();

      formContext.getControl("Name of field B").setVisible(false);

    formContext.getControl("Name of field C").setVisible(false);

    formContext.getControl("Name of field D").setVisible(false);

      var fieldA = formContext.getAttribute("Name of A field").getText();

      if(fieldA != null)

      {

        if(fieldA == "B")

        {

           formContext.getControl("Name of field B").setVisible(true);    

        }

        elseif(fieldA == "C")

        {

            formContext.getControl("Name of field C").setVisible(true);

        }

        elseif(fieldA == "D")

        {

            formContext.getControl("Name of field D").setVisible(true);  

        }

      }

    }

  • Aneeqa Pervaiz Profile Picture
    240 on at

    Hello.

    So this is my code:

    function new_hideFields(executionContext)

    {

    var formContext = executionContext.getFormContext();

    if (formContext.getControl('header_process_amp_appointmentlink') != null)

    formContext.getControl('header_process_amp_appointmentlink').setVisible(false);

    if (formContext.getControl('header_process_amp_emaillink') != null)

    formContext.getControl('header_process_amp_emaillink').setVisible(false);

    if (formContext.getControl('header_process_amp_phonecalllink') != null)

    formContext.getControl('header_process_amp_phonecalllink').setVisible(false);

    var introduction = formContext.getAttribute('amp_introductiontype').getText();

    if(introduction != null)

    {

    if(introduction == "Appointment")

    {

    formContext.getControl('amp_appointmentlink').setVisible(true);

    }

    else if (introduction == "Email")

    {

    formContext.getControl('amp_emaillink').setVisible(true);

    }

    else if (introduction == "Phone Call")

    {

    formContext.getControl('amp_phonecalllink').setVisible(true);

    }

    }

    }

    When I make any changes and refresh the page, I get the error that "Cannot read property 'SetVisible' of null.

    What is the error that I am making here?

  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Hello,

    I would highly recommend to master basics of troubleshooting - www.youtube.com/watch

    It's really hard to figure out what and where is going wrong without an access to environment.

  • Aneeqa Pervaiz Profile Picture
    240 on at

    The code is working fine as long as only the fields are to remain hidden. Then, when the part of the code where the visibility has to set to true on condition, i think that is where the error is.

  • Verified answer
    Bipin D365 Profile Picture
    28,983 Moderator on at

    Hi,

    It is best practice to check if control is available on the form. I can see you have added check condition for some part of the code.

    This is also helpful when somebody remove the field from the form, in this case end user will start getting error message when you don't check null.

    To do that see below code.

    if(formContext.getControl('header_process_amp_emaillink')!=null)

    better to add above check on below lines of code as well.

    if(introduction == "Appointment")

    {

    formContext.getControl('amp_appointmentlink').setVisible(true);

    }

    else if (introduction == "Email")

    {

    formContext.getControl('amp_emaillink').setVisible(true);

    }

    else if (introduction == "Phone Call")

    {

    formContext.getControl('amp_phonecalllink').setVisible(true);

    }

    If found helpful, Please mark my answer verified.

  • Aneeqa Pervaiz Profile Picture
    240 on at

    Hey Bipin,

    Thank you for the helpful tip of adding control in the codes. It no longer gives me an error. But, the problem still persists.

    1. Initially, the fields are made hidden.

    2. Then they do become visible depending on what was chosen as the option.

    3. When you refresh or reopen the form, it disappears again.

    There is just this one code on the form, no Business Rules or any workflows.

    What can I do to ensure that the fields made visible after choosing the option does not become hidden again.

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
Pallavi Phade Profile Picture

Pallavi Phade 98

#2
Tom_Gioielli Profile Picture

Tom_Gioielli 60 Super User 2025 Season 2

#3
Gerardo Rentería García Profile Picture

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

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans