cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Billy_C
Helper V
Helper V

Model Driven Apps hiding/showing fields based on Multiple choice column

Hi there,


I have been trying to hide or show multiple columns based on a multiple choices column. I first tried to use the business rules to check if it contains a certain value, but since multiple-choice fields aren't displayed in business rules I can't show or hide my fields.

So I was thinking to use separate calculated yes/no fields to check if that field contained that specific value or not.

Again in calculated fields you can't choose a multiple choice field for conditions.

To give you an example I have a section called related info where I want to connect my reminder with multiple types of tables (entities). So I have a column (field) called Applicable to which is an option set that contains different options as Company, Contact Person, Project, Quote, Order, Delivery, Application, Invoice, Payment.

A user can select multiple fields based on what they want to connect that record with that way only the necessary fields are visible so not to many fields are visible at the time and the form stays nice and clean.


So I wonder is there a method to be able to hide/show lookup columns based on a multiple choice field?


Thanks in advance,


Billy Cottrell

1 ACCEPTED SOLUTION

Accepted Solutions
Mira_Ghaly
Dual Super User
Dual Super User

@Billy_C 

To be able to deal with multiselect option set you need to use JavaScript please find below sample Javascript:

 

function showHideFiels(executionContext) {
    var formContext = executionContext.getFormContext();
    var type = Xrm.Page.getAttribute("fieldschemaname").getValue();
  if (type != null) {
  if (type.indexOf(1) != -1)//
        {
//showfield
            formContext.getControl("fieldSchemaname").setVisible(true);

}
else
{
//Hide field
            formContext.getControl("fieldSchemaname").setVisible(true);

}
}
}

 To set the JavaScript to work on the field change: Please follow the below steps:

1. Open the form and click on Events -> Add Library

Mira_Ghaly_0-1618577887305.png

2. Create New and add a new JavaScript file and paste the function in it

3. Click on event handler select the JavaScript file and Tick Pass execution context as first object

Mira_Ghaly_1-1618578035319.png

 

If this post helps you with your problem, please mark your as Accepted solution.If you like my response, please give it a Thumbs Up.

Blog: here

View solution in original post

4 REPLIES 4
Mira_Ghaly
Dual Super User
Dual Super User

@Billy_C 

To be able to deal with multiselect option set you need to use JavaScript please find below sample Javascript:

 

function showHideFiels(executionContext) {
    var formContext = executionContext.getFormContext();
    var type = Xrm.Page.getAttribute("fieldschemaname").getValue();
  if (type != null) {
  if (type.indexOf(1) != -1)//
        {
//showfield
            formContext.getControl("fieldSchemaname").setVisible(true);

}
else
{
//Hide field
            formContext.getControl("fieldSchemaname").setVisible(true);

}
}
}

 To set the JavaScript to work on the field change: Please follow the below steps:

1. Open the form and click on Events -> Add Library

Mira_Ghaly_0-1618577887305.png

2. Create New and add a new JavaScript file and paste the function in it

3. Click on event handler select the JavaScript file and Tick Pass execution context as first object

Mira_Ghaly_1-1618578035319.png

 

If this post helps you with your problem, please mark your as Accepted solution.If you like my response, please give it a Thumbs Up.

Blog: here
Billy_C
Helper V
Helper V

Hi @Mira_Ghaly,

 

So I have been trying to figure this out but for some reason it doesn't want to work so this is the following code I have created so far:

 

function showHideFields(executionContext) {
    var formContext = executionContext.getFormContext();
    var type = Xrm.Page.getAttribute("ika_applicableto").getValue();
    if (type != null) {
        switchvisibility(formContext, type, 966750000, "ika_company");
        switchvisibility(formContext, type, 966750001, "ika_contact");
        switchvisibility(formContext, type, 966750002, "ika_project");
        switchvisibility(formContext, type, 966750003, "ika_quote");
        switchvisibility(formContext, type, 966750004, "ika_order");
        switchvisibility(formContext, type, 966750005, "ika_delivery");
        switchvisibility(formContext, type, 966750006, "ika_application");
        switchvisibility(formContext, type, 966750007, "ika_invoice");
        switchvisibility(formContext, type, 966750008, "ika_payment");
    }
}

function switchvisibility(formContext, type, id, name) {
    if (type.indexOf(id) != -1) {
        //show field
        formContext.getControl(name).setVisible(true);
    } else {
        //hide field
        formContext.getControl(name).setVisible(false);
    }
}

 

So I mean it does work to some extent like when changing a value and when loading the form. The main issue is that when the value is null then its doesn't do anything.

I tried different methods to do this but it doesn't want to update that visibility.
Any ideas on this?

 

Thanks in advance,

 

Billy Cottrell

Billy_C
Helper V
Helper V

Hi @Mira_Ghaly,

 

I finally managed to get it to work this is the code that I am currently using:

function showHideFields(executionContext) {
    var formContext = executionContext.getFormContext();
    var type = Xrm.Page.getAttribute("ika_applicableto").getValue();
    var list = [
        {id: 966750000, name: "ika_company"},
        {id: 966750001, name: "ika_contact"}, 
        {id: 966750002, name: "ika_project"},
        {id: 966750003, name: "ika_quote"},
        {id: 966750004, name: "ika_order"},
        {id: 966750005, name: "ika_delivery"},
        {id: 966750006, name: "ika_application"},
        {id: 966750007, name: "ika_invoice"},
        {id: 966750008, name: "ika_payment"}
    ];
    if (type != null) {
        for (var i = 0; i < list.length; i++) {
            formContext.getControl(list[i].name).setVisible(type.indexOf(list[i].id) != -1 ? true : false);
        }
    } else {
        for (var i = 0; i < list.length; i++) {
            formContext.getControl(list[i].name).setVisible(false);
        }
    }
}

Sorry for the previous post, it's the first time I coded something for powerapps so I didn't know how it was working. I mean I am used to coding but I just didn't know how Powerapps was passing its data nor what data it actually was.

 

Anyways it works, thanks for all the help!

 

Kind regards,

 

Billy Cottrell

This was very helpful!

 

🙂

Helpful resources

Announcements
October Events

Mark Your Calendars

So many events that are happening this month - don't miss out!

Ignite 2022

WHAT’S NEXT AT MICROSOFT IGNITE 2022

Explore the latest innovations, learn from product experts and partners, level up your skillset, and create connections from around the world.

Power Apps Africa Challenge 2022

Power Apps Africa Challenge

Your chance to join an engaging competition of Power Platform enthusiasts.

Users online (2,544)