Set multiple-choice variable default value if more than one records show up in list collector field

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-13-2023 06:13 AM
I have a catalog item where I want to set the default value of a multiple-choice field based on the data that shows up on another list collector (this data varies based on another reference field) field.
My multiple-choice field has 3 options (Option 1, Option 2, and Option 3).
If the list collector field displays one record in the drop-down, I want Option 1 to be selected by default for the multiple-choice field.
If the list collector field displays more than one record in the drop-down, I want Option 3 to be selected by default for the multiple-choice field.
Please let me know if anyone can think of a way to implement this.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-13-2023 07:01 AM - edited ‎06-13-2023 07:03 AM
HI @Community Alums ,
Hope you are doing great.
You can achieve this using onchange catalog client script on list collector field. TRy using below script and modify it based on you use case:
function onChangeListCollector() {
var listCollectorField = g_form.getReference('list_collector_field'); // Replace 'list_collector_field' with the actual field name
var multipleChoiceField = g_form.getControl('multiple_choice_field'); // Replace 'multiple_choice_field' with the actual field name
if (listCollectorField.length === 1) {
// Set Option 1 as the default value for the multiple-choice field
multipleChoiceField.value = 'Option 1';
} else if (listCollectorField.length > 1) {
// Set Option 3 as the default value for the multiple-choice field
multipleChoiceField.value = 'Option 3';
}
}
Regards,
Riya Verma

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-13-2023 07:54 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-13-2023 09:37 AM
@Community Alums, Not sure if I understood your requirement correctly. In my PDI I had created a list collector which will list all the active users, and a multi choice variable with the choices 1- No Records, 2 - One Record, 3- Multiple Records.
Created a client callable script include "return_count"
function return_count(query) {
gs.addErrorMessage('In Script Include123');
gs.addErrorMessage(query);
var sys_user_rec = new GlideRecord('sys_user');
sys_user_rec.addEncodedQuery(query);
sys_user_rec.query();
if (sys_user_rec.next()) {
var row_count = sys_user_rec.getRowCount();
gs.info(row_count);
if (row_count > 1) {
return 3;
} else {
return 2;
}
} else {
return 1;
}
}
In the multi choice field called the script from the default value section
javascript:return_count('active=true');
Please see the results, when the query was "active=true", Multiple records was auto selected
When the query was 'user_name=abel.tuter' one record was auto selected.
When the query was 'user_name=123', no records was auto selected.
Please mark the appropriate response as correct answer and helpful, This may help other community users to follow correct solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-14-2023 01:45 AM
Hi @Arun_S1 Thanks for all your effort. I tired it, but still no luck. Not sure if I'm doing something wrong, here is my script include
var SiteCount = Class.create();
SiteCount.prototype = Object.extendsObject(AbstractAjaxProcessor, {
initialize: function return_count(query) {
gs.addErrorMessage('In Script Include123');
gs.addErrorMessage(query);
var loc_rec = new GlideRecord('cmn_location');
loc_rec.addEncodedQuery(query);
loc_rec.query();
if (loc_rec.next()) {
var row_count = loc_rec.getRowCount();
gs.info(row_count);
if (row_count > 1) {
return 3;
} else {
return 1;
}
}
}
});

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-14-2023 01:57 AM