web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / Change a form dependin...
Power Apps
Unanswered

Change a form depending on a choice field in a model driven app

(0) ShareShare
ReportReport
Posted on by 5

Hello all,
 
I'm creating a model driven app, in the contacts table I have three main types of contacts: students, teachers, and providers. For each contact type I have a different form that shows several fields/columns and subgrids which is only for that type of contact (eg a student has classes enrolled in, a teacher has classes they teach etc). I have a choice column called 'Contact Type' for the contact table and was wondering if I can use this to switch the form that is used for that contact type? (Eg if the contact type is 'student' then the 'student form' is used)
 
Is this possible, and if so how do I do this?
 
I have tried to use business rules, however I can't show or hide a sub-grid.
 
Follow up question: If this isn't possible, could I show or hide tab that depends on the contact type? (ie I put all of the student specific info and columns into on tab that only shows when student is selected)

 

Categories:
I have the same question (0)
  • Verified answer
    Ethan_009 Profile Picture
    4,838 Moderator on at

    Hi @mitp ,

     

    You can use JavaScript during Form OnLoad, add the library and connect the Script onLoad

    // Use the Script as NameSpace.switchForm | Modify as per your style
    
    var NameSpace = 
    {
     switchForm: function(executionContext)
     {
     // Contact Type: Student <optionset value (100000) | Student Form GUID = 123400ab-bc90-28hb-v2n3-0000abcd0000
     // Contact Type: Teacher <optionset value (100001) | Teacher Form GUID = 567800ab-bc90-28hb-v2n3-123900024fb9
     ....
     
     var formContext = executionContext.getFormContext();
     if(formContext.data.entity.getEntityName() != "contact")
     {
     var listOfAvailableForms = formContext.ui.formSelector.items.get();
     var currentForm = formContext.ui.formSelector.getCurrentItem().getId();
     
     if(formContext.getAttribute("new_contacttype"))
     {
     var ContactType = formContext.getAttribute("new_contacttype").getValue();
     //Check if the form is Student
     if(ContactType == "100000" && currentForm != "123400ab-bc90-28hb-v2n3-0000abcd0000")
     {
     listOfAvailableForms.forEach(element => {
     if(element.getId() == "123400ab-bc90-28hb-v2n3-0000abcd0000")
     element.navigate();
     });
     }
     //Check if the Form is Teacher
     else if(ContactType == "100001" && currentForm != "567800ab-bc90-28hb-v2n3-123900024fb9")
     {
     listOfAvailableForms.forEach(element => {
     if(element.getId() == "567800ab-bc90-28hb-v2n3-123900024fb9")
     element.navigate();
     });
     }
     }
     }
     }
    }

     

    Make sure you have entered the Form GUID and OptionSet Values respectively

     

    You can also show/hide Tabs using JS.

     

    Hope this helps

  • mitp Profile Picture
    5 on at

    Thanks 🙂

  • SpeechAndLang Profile Picture
    5 on at

    Hi @Ethan_R ,

     

    I'm trying to implement this on the opportunity entity. The Form GUID, OptionSet Values and Entity Name have been updated in the js file before uploading.

    When this is added to the form on Load with the Function NameSpace.switchForm, we're experiencing the below error:

     

    Web resource method does not exist: NameSpace.switchForm

    Event Name: onload

    Function Name: NameSpace.switchForm

    FormWeb Resource Name: words_OpportunityFormDisplayControlSolution

     

    Do you have any ideas on how to resolve this? 

  • Ethan_009 Profile Picture
    4,838 Moderator on at

    Hi @SpeechAndLang ,

     

    Try the following steps:

    1. Update your JS once again and make sure you Save & Publish all customization. 

    2. Go to the opportunity record where you are trying the code

    3. If you are debugging the JS by keeping breakpoints then remove it, else ignore this step.

    4. Refresh the page for decent amount like 5-10 times so the latest customization will take place.

     

    Hope this helps

  • SpeechAndLang Profile Picture
    5 on at

    Hi @Ethan_R 

     

    That worked a treat, thank you! The issue was with the breakpoints, it's my first time using any JS as we try to stay no code as much as possible.

     

    Thanks for the help! 

  • ArthurDuplouy Profile Picture
    2 on at

    Hello Ethan,

    Thanks for your solution. Being a novice, I am kind of lost with using your script.

    We have 4 type of clients which we called: Tenant, Landlord, Furniture and Construction. I would like to have a Lead form specific for each client (fields and BPFs are different). We are using a field called Purchase Process to select the type of client. The idea is to have this field in the Quick Create, and once we create the lead it automatically chooses the right form. We also need to keep the same structure once we qualify the Lead into an Opportunity.


    Could you help me please?

     

    Thanks.

    Arthur.

  • RobSchootman Profile Picture
    Microsoft Employee on at

    Hi @Ethan_R ,

    I still get the error message Web Resource method does not exist. I copied your script and changed the forms and option set values. Somehow the NameSpace.switchForm is not recognized. I've enabled the event as shown below.

    RobSchootman_0-1705416009889.png

    Hope you can help. Many thanks.

     

  • Ethan_009 Profile Picture
    4,838 Moderator on at

    Hi @RobSchootman ,

     

    Please go through some points which will help you review the implementation:

    1. Your script (JS as web resource) must be included in the Form libraries that you are executing the code.
    2. When you provide your function name, make sure you select the correct Library (JS file) from where it has to find the function.
    3. If you have used 'NameSpace.switchForm' as trigger function then this should be present in your JS code file. (Note: It is case sensitive and may fail if space or case is different)
    4. Finally, do check if syntax or code error exists since it may throw same error if it cannot detect proper JS code.

    These points can help you find the issue 

     

    Also, finally to check if your script is valid then try debugging as follows:

    (To check valid JS, this link helps : https://esprima.org/demo/validate.html )

    1. Open your Form and open your Console Page (Ctrl+Shift+I or F12) and open your JS file in your source page.
    2. If you find your JS file then file is valid but some syntax or spelling error exists.

     

    Do try out and hope this helps

  • RobSchootman Profile Picture
    Microsoft Employee on at

    Hi @Ethan_R 

    Thanks for the quick reply. I used your Jscript, just updated the GUID, Optionset values an Optionset naming. I published the JS file in the Library an then used  the Event OnLoad. As function trigger I used NameSpace.switchForm, I assume that is the right one or should I use another trigger?

     

    Thanks again for your help.

  • Ethan_009 Profile Picture
    4,838 Moderator on at

    Hi @RobSchootman ,

     

    Trigger 'onLoad' will work when you open the Form related to a record (If you have many forms then script will trigger for those for which you added OnLoad) 

     

    Triggers can change based on your use-case, selecting correct trigger depends on the correct logic.

    Based on how fields accepts input and provides output, you need to handle the items along with Form functions, Xrm WebAPI functions and much more.

     

    If there's any anything, do comment.

    Hope this helps 

     

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

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Apps

#1
WarrenBelz Profile Picture

WarrenBelz 717 Most Valuable Professional

#2
Michael E. Gernaey Profile Picture

Michael E. Gernaey 329 Super User 2025 Season 2

#3
Power Platform 1919 Profile Picture

Power Platform 1919 268

Last 30 days Overall leaderboard