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.,
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
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:
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.
Most Viewed Articles (in Javascript )
Dynamically modify the option set in Dynamics 365 forms
promise and .then() in JavaScript
reduce() and filter() in JavaScript
History and evolution of Javascript
Using parseInt() and parseFloat() in JavaScript to convert data types to Numbers
Show how many characters remaining in a html text box using javascript
Latest Articles (in Javascript)
© 2023 Java-samples.com
Tutorial Archive: Data Science React Native Android AJAX ASP.net C C++ C# Cocoa Cloud Computing EJB Errors Java Certification Interview iPhone Javascript JSF JSP Java Beans J2ME JDBC Linux Mac OS X MySQL Perl PHP Python Ruby SAP VB.net EJB Struts Trends WebServices XML Office 365 Hibernate
Latest Tutorials on: Data Science React Native Android AJAX ASP.net C Cocoa C++ C# EJB Errors Java Certification Interview iPhone Javascript JSF JSP Java Beans J2ME JDBC Linux Mac OS X MySQL Perl PHP Python Ruby SAP VB.net EJB Struts Cloud Computing WebServices XML Office 365 Hibernate