web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
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 

I have the same question (0)
  • Moiz Raj Profile Picture
    5 on at

    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.

  • Suggested answer
    Henry J. Profile Picture
    5,237 on at

    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
    5 on at

    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
    Linn Zaw Win Profile Picture
    3,407 on at

    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
    5 on at

    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 :(

  • Linn Zaw Win Profile Picture
    3,407 on at

    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.

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

#1
Martin Dráb Profile Picture

Martin Dráb 47 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 38 Super User 2025 Season 2

#3
Pallavi Phade Profile Picture

Pallavi Phade 32

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans