Yesterday I had the requirement of making several fields on a Tab Business Required, the Tab contained a lot of fields and I didn’t wanted to hardcode all of them, also I didn’t want to change the Javascript every time a new field is added.
So I googl I mean Binged how to get all the fields from a Tab, and… I couldn’t find it: S
The closest I got was to get the all the fields of Section and from there I just went one level up.
Code:
Steps:
- First we get all the controls on the Form.
- Navigate through the controls and get the Tab where they are.
- In order to do that we need to go 2 levels up, so the trick is control.getParent().getParent().getLabel() //The first getParent takes us to the section and the second one to the Tab.
- We compare the name of the Tab with the one we need.
- In order to set as Business required a field we can’t use the control, we need the attribute, so we need to get the name of the control first, and then we can set the Required Level.
—-
UPDATE:
Thanks to Rhett Clinton (@rhettclinton) for this update 😉
Xrm.Page.ui.tabs.get(tabName).sections.forEach(function (section, sectionIndex) {
section.controls.forEach(function (control, index) {
control.getAttribute().setRequired(“required”);
});
});
—-
Hope it helps,
Cheers,
Mc
Hello Mario, you could also iterate through the desired tab’s sections and then its controls to perform the same action.
Xrm.Page.ui.tabs.get(tabName).sections.forEach(function (section, sectionIndex) {
section.controls.forEach(function (control, index) {
control.getAttribute().setRequired(“required”);
});
});
Cheers,
Rhett
Thanks Rhett! That is great, I will update the post to include your comments 😉
Cheers,
Mc