Task- 7 _ Removing and Adding Options to Option Set in Dynamics 365

 Removing and Adding Options to Option Set

Create a case DMPS type yes or no field, if yes then show case origin only DMPS or show other options except DMPS.

User Story: -

Create a DMPS two-option set field with yes or no options in the case entity, based on the DPMS field value if it is Yes then in the origin option set it should only contain the DMPS option, if it is No then it should contain the remaining options except DMPS.

Sudo code: -

  • Create a DPMS two-option field with YES or No as options.
  • Create a JavaScript file with a function to update the option set options.
    • In the JavaScript code first, retrieve the DMPS field value, it will return true or false.
    • Get all the options by the getOptions method.
    • Use the foreach loop on the retrieved options then remove the each option from the origin option set by the removeOption method.
    • If DMPS is true then add the DMPS option to the origin option set.
    • If DMPS is false then add the remaining options to the origin option set
  • Create a JavaScript Web resource with this code.
  • Create an OnChange event for DMPS field, and also create an OnLoad event to the case entity main form.

JavaScript code : -


function OriginOptionsUpdate(executionContext) {
    var formContext = executionContext.getFormContext();


    var options = formContext.getControl("caseorigincode").getOptions();
    // it will retrive array of option objects with name and value attributes.

    //removing all options
    options.forEach(option => { formContext.getControl("caseorigincode").removeOption(option.value) })

    // or we can use formContext.getControl(arg).clearOptions("caseorigincode"); to clear all options


    var Is_DMPS_Enabled = formContext.getAttribute("new_dmps").getValue();

    var FalseOptions = [{ text: "Phone", value: 1 }, { text: "Email", value: 2 }, { text: "Web", value: 3 },
    { text: "Facebook", value: 2483 }, { text: "Twitter", value: 3986 }, { text: "IoT", value: 700610000 }]

    var TrueOptions = [{ text: "DMPS", value: 0 }]

    if (Is_DMPS_Enabled) {

        TrueOptions.forEach(Option => { formContext.getControl("caseorigincode").addOption(Option); })
    }
    else {

        FalseOptions.forEach(Option => { formContext.getControl("caseorigincode").addOption(Option) });

    }

}















Comments

Popular posts from this blog

How to create an Azure AD(Active Directory) and create an application to connect Dynamics 365 from console application

Task -6 JavaScript , Regular expressions

Task_1 JavaScript