Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

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

Not applicable

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

12 REPLIES 12

Riya Verma
Kilo Sage

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';
    }
}

 

Please mark the appropriate response as correct answer and helpful, This may help other community users to follow correct solution.
Regards,
Riya Verma

Not applicable

Hi @Riya Verma,  Thanks for the response.

 

I tried, but it did not work too.

Thanks

Arun_S1
Tera Guru

@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');

Arun_S1_0-1686673904853.png

 

Please see the results, when the query was "active=true", Multiple records was auto selected

Arun_S1_2-1686674049247.png

 

When the query was 'user_name=abel.tuter' one record was auto selected.

Arun_S1_3-1686674134549.png

 

When the query was 'user_name=123', no records was auto selected.

Arun_S1_4-1686674209836.png

 

Please mark the appropriate response as correct answer and helpful, This may help other community users to follow correct solution.

 

 

 

 

 

Not applicable

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;
                }
            } 
        }
    });

Not applicable

RJ8_1-1686733056310.png