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

  • Aneeqa Pervaiz Profile Picture
    Aneeqa Pervaiz 240 on at
    RE: Hiding field in BPF until another field is filled in

    Thank you so much for your help. It finally works now.

  • Verified answer
    Abby Kong Profile Picture
    Abby Kong 6 on at
    RE: Hiding field in BPF until another field is filled in

    Hi Aneeqa,

    Assuming Introduction Type is an optionset field:

    if (formContext.getAttribute("amp_introductiontype").getText() == "Appointment")

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

    Regards,

    Abby

  • Aneeqa Pervaiz Profile Picture
    Aneeqa Pervaiz 240 on at
    RE: Hiding field in BPF until another field is filled in

    Hey Abby,

    What would be a way around it then? Would this be a correct line?

    if (formContext.getControl('amp_introductiontype') == Appointment)

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

    If not this, how would the correct line look like??

    Would appreciate the help.

    Thanks.

  • Abby Kong Profile Picture
    Abby Kong 6 on at
    RE: Hiding field in BPF until another field is filled in

    Hi Aneeqa,

    Looking at code condition, it's comparing Control to Text, this would never be true.

    if (formContext.getControl(introduction) == "Appointment")   // will never be true

    {

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

    }

    else if (formContext.getControl(introduction) == "Email")   // will never be true

    {

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

    }

    else if (formContext.getControl(introduction) == "Phone Call")    // will never be true

    {

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

    }

    Regards,

    Abby

  • Abby Kong Profile Picture
    Abby Kong 6 on at
    RE: Hiding field in BPF until another field is filled in

    Hi Aneeqa,

    I would follow Andrew's recommendation of not using mixture of JS and Business Rules.

    Sometimes fields changed from business rule getting triggered doesn't fire JS onchange.

    community.dynamics.com/.../onchange-script-doesn-t-work-when-field-changes-by-a-business-rule-crm-2015

    It's best not to use mixture of JS and business rules on one form/field. In this case if business rule can't do what you need, move the logic into JS to avoid headache.

    Regards,

    Abby

  • Aneeqa Pervaiz Profile Picture
    Aneeqa Pervaiz 240 on at
    RE: Hiding field in BPF until another field is filled in

    Hello,

    So I added the 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 (formContext.getControl(introduction) == "Appointment")
    		{
    			formContext.getControl('amp_appointmentlink').setVisible(true);
    		}
    		else if (formContext.getControl(introduction) == "Email")
    		{
    			formContext.getControl('amp_emaillink').setVisible(true);
    		}
    		else if (formContext.getControl(introduction) == "Phone Call")
    		{
    			formContext.getControl('amp_phonecalllink').setVisible(true);
    		}
    	}
    }

    Under form properties in the form, I added the form in two places. 
    1. Form - OnLoad
    2. Introduction Type = OnChange

    I was using Business Rules initially to display the hidden fields based on what was chosen in Intro type. I removed them., but now the problems I am facing with code. 

    1. When the form loads, the fields are hidden as I want. But when I choose any intro type, the relevant field does not become visible. For instance, I choose Appointment as intro type, the appointment link does not become visible. Is there a problem with the part of my code that displays fields?

    2. I added a temp business rule to display one link when the intro type is chosen. Now when i fill that link in and refresh, the field disappears again. This is the only reason why I needed a code in place. Otherwise, business rules were doing the job. 

    Please tell me what I need to change. 

    Thank you. 

  • Verified answer
    a33ik Profile Picture
    a33ik 84,325 Most Valuable Professional on at
    RE: Hiding field in BPF until another field is filled in

    Aneeqa,

    This logic should be triggered on both onload and onchange of introductiontype field.

  • Aneeqa Pervaiz Profile Picture
    Aneeqa Pervaiz 240 on at
    RE: Hiding field in BPF until another field is filled in

    Hey,

    I was thinking - does this have something to do with where my codes are loaded?

    Should I divide this in two parts. the part to hide fields should be on Form(Load) - and the part to make fields visible should be on Form(Save) ?  But then that will also halt the process. What do you suggest?

  • Aneeqa Pervaiz Profile Picture
    Aneeqa Pervaiz 240 on at
    RE: Hiding field in BPF until another field is filled in

    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.

  • Verified answer
    Bipin D365 Profile Picture
    Bipin D365 28,961 Moderator on at
    RE: Hiding field in BPF until another field is filled in

    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.

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

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Tip: Become a User Group leader!

Join the ranks of valued community UG leaders

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,399 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans