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
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.
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 :(
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
Then, before adding options, clearOptions() to clear the dummy values
this.formContext.getControl('new_roleoptions').clearOptions();
I hope that works.
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.
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
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.
André Arnaud de Cal...
291,969
Super User 2025 Season 1
Martin Dráb
230,842
Most Valuable Professional
nmaenpaa
101,156