Dynamically modify the option set in Dynamics 365 forms
By: Terrence in Javascript Tutorials on 2023-08-22
To dynamically modify the option set in Dynamics 365 forms, either on Form Load or on a specific field OnChange event, we can replace the options in the option set with the allowed options. This will provide a consistent and reliable behavior.
Assuming you have a status field name ss_status with six options and you want to filter the available options for the user when they load the form or when the value of the status field changes, then you can create a JavaScript like below and add it in the 'Form Properties' and choose the function in Form OnLoad as well as on the OnChange event of ss_status field.
function setAllowedOptionsBasedOnStatus(executionContext) { var formContext = executionContext.getFormContext(); var statusField = formContext.getAttribute("ss_status"); // Define the allowed options based on the current value of ss_status var currentStatus = statusField.getValue(); var allowedOptionValues = []; // Define allowed options based on the current status if (currentStatus === 717800005) { allowedOptionValues = [717800000,717800005,717800005]; statusField.setValue(717800000); } else if (currentStatus === 717800000) { allowedOptionValues = [717800001, 717800002,717800005]; } else if (currentStatus === 717800001) { allowedOptionValues = [717800001, 717800002]; } else if (currentStatus === 717800002) { allowedOptionValues = [717800002]; } else if (currentStatus === 717800003) { allowedOptionValues = [717800003]; } else if (currentStatus === 717800004) { allowedOptionValues = [717800004]; } else if (currentStatus === 717800006) { allowedOptionValues = [717800006,717800005]; } // Get the control instance of the status field var statusFieldControl = formContext.getControl("ss_status"); // Get the attribute options var options = statusFieldControl.getAttribute().getOptions(); // Clear existing options statusFieldControl.clearOptions(); // Add only allowed options back for (var i = 0; i < options.length; i++) { var option = options[i]; if (allowedOptionValues.indexOf(option.value) !== -1) { statusFieldControl.addOption(option); } } }
In this function, in addition to restricting the available options in the option set based on the current status of the ss_status field, I am also setting the value of the field to a certain option for the first option. This is just my requirement and this can be removed in your case.
if (currentStatus === 717800005) { allowedOptionValues = [717800000,717800005,717800005]; // statusField.setValue(717800000); }
You will have to check the actual values for your option set and replace it with your own values instead of 717800000 etc.,
Add Comment
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
- Your name, rating, website address, town, country, state and comment will be publicly displayed if entered.
- Aside from the data entered into these form fields, other stored data about your comment will include:
- Your IP address (not displayed)
- The time/date of your submission (displayed)
- Your email address will not be shared. It is collected for only two reasons:
- Administrative purposes, should a need to contact you arise.
- To inform you of new comments, should you subscribe to receive notifications.
- A cookie may be set on your computer. This is used to remember your inputs. It will expire by itself.
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
- Although the administrator will attempt to moderate comments, it is impossible for every comment to have been moderated at any given time.
- You acknowledge that all comments express the views and opinions of the original author and not those of the administrator.
- You agree not to post any material which is knowingly false, obscene, hateful, threatening, harassing or invasive of a person's privacy.
- The administrator has the right to edit, move or remove any comment for any reason and without notice.
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
Comments