Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Unanswered

Help with Javascript - Certain Criteria will show certain sections in 4 different forms

Posted on by 10
I would like to start by saying that I am fairly new to coding in general.
 
I've been working on a project that is needing me to make certain sections in a few (4) different form tabs (details) available for different criteria. Ex. If a "Product sub-type" eq "X" then they can see sections X, Y, Z but not A & B. (Which a different subtype can see all, etc..)
 
The way I built it out is that there is a field option called "Setup Type" that contains choices that are basically templates to the sections they can see (Also, Business rules to auto fill the setup type choice so that users don't have to do this).
 
There is a similar process already in place at my company, but different name and users need to manually select the option they need so I used that as a base to try to make this work. I am only adding the setup function and all related javascript to it. There is the same javascript for maintenance sections. (Initialize function and iterate function)
 
All of the following functions are called on load, With the check Section Visibility function set to on change (with should trigger when my business rules change the choice for setup type field right?)
 
initializeSetupSectionDependencies = function (executionContext) {
'use strict';
//Initialize formContext
var formContext = executionContext.getFormContext();
//If the json has not been loaded yet
if (config === null) {
var clientUrl = formContext.context.getClientUrl();
//Collect path to web resource
var setupWR = clientUrl + "/WebResources/" + "new\_setupDependencyJSON.js";
var xhr = new XMLHttpRequest();
xhr.open("GET", setupWR, true);
xhr.onreadystatechange = function () {
if (this.readyState == 4) { //complete
this.onreadystatechange = null;
if (this.status == 200) { //success response
config = JSON.parse(this.response); //parse json to variable
checkSectionVisibility(executionContext);
}
else {
throw new Error("Failed to load configuration data for dependent section sets.");
}
}
};
xhr.send(); //send
}
else {
checkSectionVisibility(executionContext);
}
}
checkSectionVisibility = function (executionContext) {
'use strict';
if (config === null) {
throw new Error("Failed to load configuration data for dependent section sets.");
}
else {
var formContext = executionContext.getFormContext();
var requestType = formContext.getControl("new\_requesttype") ? formContext.getAttribute("new\_requesttype").getValue()\[0\].name : null;
if (requestType === null) return;
for (var i = 0; i < config.length; i++) {
var dependentOptionSet === config\[i\];
for (var j = 0; j < dependentOptionSet.requestType.length; j++) {
if (dependentOptionSet.requestType[j] === requestType) {
setAvailableMaintenanceTypes(formContext, dependentOptionSet);
iterateThroughSections(formContext, dependentOptionSet);
}
}
}
}
}
iterateThroughSetupSections = function (formContext, sectionDependencies) {
'use strict';
if (config === null) {
throw new Error("Failed to load configuration data for dependent sections sets.");
}
else {
var serviceType = formContext.getControl("new\_servicerequesttype") ? formContext.getAttribute("new\_servicerequesttype").getValue() : null;
var setup = false;
//If service type is new setup, set setup type section to visible
if (serviceType === 100000000) {
setup = true;
.setSectionVisibility(formContext, sectionDependencies.tabName, "sec\_setuptypes", setup);
}
//If service type is maintenance, set setup type section to hidden
else if (serviceType === 100000001) {
setup = false;
setSectionVisibility(formContext, sectionDependencies.tabName, "sec\_setuptypes", setup);
}
//Iterate through each section for the current request type
for (var i = 0; i < sectionDependencies.sections.length; i++) {
var show = false;
var setupType = formContext.getControl("new\_setuptype") ? formContext.getAttribute("new\_setuptype").getValue() : null;
var name = sectionDependencies.sections\[i\].name;
//If current section should be shown regardless of anything else
if (name === "always\_shown") {
show = true;
}
//Show section if any of the dependent setup types are selected
else {
for (var j = 0; j < sectionDependencies.sections[i].setupType.length; j++) {
var number = parseInt(sectionDependencies.sections[i].setupType[j]);
if (setupType.includes(number)) {
show = true;
}
}
}
setSectionVisibility(formContext, sectionDependencies.tabName, name, show);
}
}
}
.setSectionVisibility = function (formContext, tabName, sectionName, visibility) {
'use strict';
if (sectionName != "no\_section") {
var tab = formContext.ui.tabs.get(tabName);
var section = tab.sections.get(sectionName);
section.setVisible(visibility);
}
}

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans