Skip to main content

Notifications

Microsoft Dynamics 365 | Integration, Dataverse...
Suggested answer

Setting dynamic value on a dynamic option set

(0) ShareShare
ReportReport
Posted on by 5

Hi, I have created a blank option set and using it on a form in a model driven app. Using JS I am dynamically adding all available Security roles as options. Also when saving the form, I reset the value to empty as it doesn't allow me to save a value which does not exist on the option set. To solve this I have another text field, hidden on the form where I save the roleId. 

To load the selected role value on an existing record, i am trying to set the value of this dynamics option set to the role that was saved on the hidden field. This is for some reason doesn't allow me to. Please suggest if there is an alternate for this. 

// Form load event
this.onFormLoaded = async function(executionContext) {
    this.formContext = executionContext.getFormContext();
    await Xrm.WebApi.retrieveMultipleRecords('role').then(this.onRolesFetchCallback, this.errorCallbackor);
    let selectedRole = this.formContext.getControl('new_selectedrole').getAttribute().getValue();
    roles.forEach((role, index) => {
        this.formContext.getControl('new_roleoptions').addOption({text: role.name, value: index})
    });
    if(selectedRole) {
        let role = roles.find(role => role.roleid === selectedRole)
        if(role) {
            this.formContext.getControl('new_roleoptions').getAttribute().setValue(roles.indexOf(role));
        }
    }
}

// Callback when all the security roles are fetched
this.onRolesFetchCallback = function(result) {
    roles = result.entities;
}

// Set the selected role from dynamic role options to the hidden selectedRole text field
this.onRoleSelected = function(executionContext) {
    let selectedRole = this.formContext.getControl('new_roleoptions').getAttribute().getValue();
    console.log(roles[selectedRole].roleid)
    this.formContext.getControl('new_selectedrole').getAttribute().setValue(roles[selectedRole].roleid);
}

Note - I am using Dynamics 365 and CDS instance. Also, I cannot use lookup field for Security Roles as it is turns up as a readonly field on the form which we cannot change.

Thanks 

  • Linn Zaw Win Profile Picture
    Linn Zaw Win 3,407 on at
    RE: Setting dynamic value on a dynamic option set

    If you prefer to use a lookup field, there is another workaround solution. Even though the lookup field for Security Roles is shown as a read-only field on the form which we cannot change, we can still set the values dynamically using JavaScript. As you did initially, dynamically add all available Security roles as options to a dummy empty OptionSet field and keep the GUIDs somewhere. When an option is selected, populate the name of the selected security role and respective GUID into the lookup together with the entity type "role".

    This is the code that I use to set the hardcoded value of security role to the lookup.

    formContext.getAttribute("lzw_securityrole").setValue([ { id: "104C7FDB-DBCB-48C5-9FC8-5DE079B7C18E", name: "Knowledge Manager", entityType: "role" } ]);

    And this is the outcome that I tested in my environment.

  • Moiz Raj Profile Picture
    Moiz Raj 5 on at
    RE: Setting dynamic value on a dynamic option set

    Thanks for the workaround. My only issue is if new security roles are added and are more than the defined dummy ones, they need to be updated again. Right now, if that is the limitation, I am going with mapping my security roles with teams and using teams as a lookup field :(

  • Suggested answer
    Linn Zaw Win Profile Picture
    Linn Zaw Win 3,407 on at
    RE: Setting dynamic value on a dynamic option set

    I don't think you can setValue() non-existent option to the blank OptionSet.

    The workaround solution that I suggest is to create the dummy options for new_selectedrole depending on the number of all available Security roles.

    e.g. if there are x10 available security roles in your organisation, create x20 or x30 options to cover the new security roles to be created in the future.

    Then, create the options like

    • Dummy option 0 (Optionset value = 0)
    • Dummy option 1 (Optionset value = 1)
    • ....
    • ....
    • ....
    • Dummy option 29 (Optionset value = 29)

    Then, before adding options, clearOptions() to clear the dummy values

    this.formContext.getControl('new_roleoptions').clearOptions();

    I hope that works.

  • Moiz Raj Profile Picture
    Moiz Raj 5 on at
    RE: Setting dynamic value on a dynamic option set

    So auto-complete too doesn't work in case of Security roles, sets the field as read only. Have kept PCF as my last option as I am looking for any OOB features instead. I am now going with using teams instead of Security roles and map them teams with roles but still looking for an alternate solution where Security roles need to be used. Thanks.

  • Suggested answer
    Henry J. Profile Picture
    Henry J. 5,237 on at
    RE: Setting dynamic value on a dynamic option set

    Hi Moiz,

    That's an interesting use case.

    Instead of an empty option-set, have you tried to use a simple text field with the "auto-complete" control?
    If that doesn't work, have you explored PCF controls? there are many examples available here: https://pcf.gallery/

    Henry

  • Moiz Raj Profile Picture
    Moiz Raj 5 on at
    RE: Setting dynamic value on a dynamic option set

    Note - I am using Dynamics 365 and CDS instance. Also, I cannot use lookup field for Security Roles as it is turns up as a readonly field on the form which we cannot change.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Announcing Forum Attachment Improvements!

We're excited to announce that attachments for replies in forums and improved…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,969 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 230,842 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans