Programming Tutorials

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

* Required information
1000

Comments

No comments yet. Be the first!

Most Viewed Articles (in Javascript )

Latest Articles (in Javascript)