Select only one from a List type field

Momiji
Tera Contributor

Hello! The requirement is to retain the OOB type of a field which is List but prevents users from selecting multiple values.

Is there a way to script it with an info message to select only one? Thank you

1 ACCEPTED SOLUTION

Musab Rasheed
Tera Sage
Tera Sage

You can achieve this with onchange or onsubmit client script with below code.

function onChange(control, oldValue, newValue, isLoading) {
  if (isLoading) return;

  var values = g_form.getValue('your_list_field_name').toString().split(',');

  if (values.length > 1) {
    alert('Please select only one value.');
    // Keep only the first selected item
    g_form.setValue('your_list_field_name', values[0]);
  }
}
Please hit like and mark my response as correct if that helps
Regards,
Musab

View solution in original post

6 REPLIES 6

Its_Azar
Tera Guru

Hi there @Momiji 

 

Yes, you can keep the field type as List but still restrict it to a single selection using a client script.  use an onChange client script that checks the length of selected values. If the user selects more than one, you can display an g_form.showFieldMsg() or g_form.addInfoMessage() telling them to select only one, and then clear the extra values. This way, you don’t have to change the field type to Choice.

 

If this helps kindly accept the solution 

☑️ If this helped, please mark it as Helpful or Accept Solution so others can find the answer too.




Kind Regards,

Mohamed Azarudeen Z

Developer @ KPMG

 Microsoft MVP (AI Services), India

Musab Rasheed
Tera Sage
Tera Sage

You can achieve this with onchange or onsubmit client script with below code.

function onChange(control, oldValue, newValue, isLoading) {
  if (isLoading) return;

  var values = g_form.getValue('your_list_field_name').toString().split(',');

  if (values.length > 1) {
    alert('Please select only one value.');
    // Keep only the first selected item
    g_form.setValue('your_list_field_name', values[0]);
  }
}
Please hit like and mark my response as correct if that helps
Regards,
Musab

@Momiji If my code is working, kindly accept solution and close the thread.

Please hit like and mark my response as correct if that helps
Regards,
Musab

Shashank_Jain
Kilo Sage

Hello @Momiji 

 

You can write on change client script for that :

Table : select the table

Type : On change

Field : select the field

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
	
    // Split the newValue into an array (it’s comma-separated for list fields)
    var selectedValues = newValue.split(',');

    if (selectedValues.length > 1) {
        // Show alert popup
        alert('Please select only one value.');

        // Reset the value to the first selection only
        g_form.setValue('u_test', "selectedValues[0]");
    }

}

In place of "u_test" write your field name at the end for set value.

 

Hope this helps!

I tried it on PDI and its working. Feel free to ask if you have any doubt.

 

If it helps, mark the solution as accepted.

Thank you!

If this works, please mark it as helpful/accepted — it keeps me motivated and helps others find solutions.
Shashank Jain