- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-05-2024 11:57 AM
Scope: GRC: Policy and Compliance Management
Table: Policy [sn_compliance_policy]
Field: Type [type]
The Type field is an inherited reference dropdown field from the Parent table "Document" [sn_grc_document]. It references "GRC Choices" [sn_grc_choice] table. This table has multiple sets of choices. Each "Set" corresponds to a table. For one table, it can have several Choice categories. Each "Choice Category" can correspond to a field and can have multiple choices.
For Example, OOTB, the GRC Choices table has 7 entries where Set = Policy and Choice Category = Type.
This indicates, the type field will have those 7 entries as choices in a Policy record.
This is configured by the dictionary definition of Type field, where the reference qualifier is a javascript call of an OOTB script include.
I have a requirement to limit the choices based on another field on the Policy form.
For example,
- If "Owning group" = Group A, the type field should have choices: Choice 1, Choice 2 and Choice 3 .
- If "Owning group" = Group B, the type field should have choices: Choice 1, Choice 5, Choice 6.
- If "Owning group" = Group C, then type field should have choices: Choice 6, Choice 7.
Is there a possibility to achieve this dynamically on change of Owning group?
Note:
- I created a list collector field with reference to Groups table on the GRC Choices table called "Allowed Groups".
- I selected all groups that should be able to see a particular choice value on Allowed groups field of that Choice record.
- I tried doing a dictionary override to override the existing reference qualifier script. I created my own version of the script include function to include the filter condition which reads "Set = Policy, Choice Category = Type and Allowed Groups Contains Owning Group". I called this function from the dictionary override. This works when the form loads, but does not change the reference qualifier when I change the Owning Group on the form before saving.
- I tried creating an OnChange client script that sends the group ID to a script include. This script include then gathers all choice records that match the reference qualifier and return the sys_ids and display values. On the client script, in the callback function, I clear the value for the field, then I clear all options and try to add options based on the sys_ids and display values. When executed, the field value does get cleared out, and all existing choices get removed, but the new choices are not getting added. The dropdown does not open to a list.
Please let me know if there is any correction / workaround I can apply if this is achievable.
Thanks!
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-09-2024 12:48 AM
Checked other community pages to see that addOption does not work for reference dropdown fields. Decided to use on submit script to prevent saving with choices a group is not supposed to use.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-05-2024 06:11 PM - edited 08-05-2024 06:48 PM
Hi @Kirthika D
To dynamically update choices in the Type field based on the Owning group field, you can use a combination of client-side scripting and server-side logic. Here’s a streamlined approach to achieve this:
1. Server-Side Script Include
Create a Script Include to return the filtered choices based on the Owning group field. Ensure it’s client-callable.
var PolicyChoiceUtil = Class.create();
PolicyChoiceUtil.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getFilteredChoices: function() {
var owningGroupId = this.getParameter('sys_id');
var choices = [];
var gr = new GlideRecord('sn_grc_choice');
gr.addQuery('set', 'Policy');
gr.addQuery('choice_category', 'Type');
gr.addQuery('allowed_groups', 'CONTAINS', owningGroupId);
gr.query();
while (gr.next()) {
choices.push({
sys_id: gr.getValue('sys_id'),
display_value: gr.getValue('name')
});
}
return JSON.stringify(choices);
},
type: 'PolicyChoiceUtil'
});
2. Client-Side OnChange Client Script
Implement an onChange client script for the Owning group field to call the Script Include and update the Type field choices dynamically.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
// Call the Script Include to get the filtered choices
var ga = new GlideAjax('PolicyChoiceUtil');
ga.addParam('sys_id', newValue); // Pass the owning group sys_id
ga.getXMLAnswer(function(response) {
var choices = JSON.parse(response);
var typeField = g_form.getControl('type');
// Clear existing options
g_form.clearOptions('type');
// Add new options
for (var i = 0; i < choices.length; i++) {
g_form.addOption('type', choices[i].sys_id, choices[i].display_value);
}
// Optionally, set the field value to null if no choices
if (choices.length === 0) {
g_form.setValue('type', '');
}
});
}
Ensure that:
- The Type field on the Policy form is configured to use the Script Include for its reference qualifier.
- The client script is correctly assigned to the Owning group field’s onChange event.
Please mark my response as helpful👍/accept the solution ✅if it helped you
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-06-2024 12:53 AM
Hi @Satishkumar B , thanks for your reply.
However, as I have mentioned in my note, I have already tried this approach but it doesn't work as expected.
This is what I get when I change the Owning group. The choices are not added to the list.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-09-2024 12:48 AM
Checked other community pages to see that addOption does not work for reference dropdown fields. Decided to use on submit script to prevent saving with choices a group is not supposed to use.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-09-2024 01:10 AM
@Kirthika D Could you post your resolution here so that it helps the community.