How to allow user to select only two checkbox in service catalog

Sachin G K1
Kilo Sage

Hi All,

 

I want to allow user to select only 2 checkbox and other 2 should be readonly.How to achieve this?

SachinGK1_0-1708234287647.png

 

1 ACCEPTED SOLUTION

kamlesh kjmar
Mega Sage
Mega Sage

Hi Sachin,

 

Try the below script, and make sure of below points:

1. If you are having 4 checkboxes, then you need to write 4 onChange client script one for each checkbox. But code in each script will remain the same. Only replace the backend name of checkbox with your's [That's only change needed in the script]. For me this scripts works as per your requirement. In my example I have 5 checkboxes where you can select only 2 checkboxes (any).

 

 

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }

   var checkBoxes = ["checkbox1", "checkbox2","checkbox3","checkbox4","checkbox5"]; //Replace backend name of all checkboxes
var uncheckedBoxes = [];
var checkedBoxes = [];
checkBoxes.forEach(function(item){
if(g_form.getValue(item) == "false")
uncheckedBoxes.push(item);
else
checkedBoxes.push(item);
})
if(checkedBoxes.length == 2){
uncheckedBoxes.forEach(function(item){
	g_form.setReadOnly(item,true);
});
}
else{
	uncheckedBoxes.forEach(function(item){
	g_form.setReadOnly(item,false);
});
}
   
}

 

 

kamleshkjmar_0-1708252275425.png

 

 

Let me know if this works for you.

 

Please mark my response as Correct if it solves your problem and hit Helpful if this rings the bell.

 

Thanks,

Kamlesh

View solution in original post

16 REPLIES 16

abirakundu23
Mega Sage

Hi @Sachin G K1 ,

You have to create catalog UI policy as per below logic on your check box option.

Please find below screenshot as ref.

akadu23_0-1708237746615.png

akadu23_1-1708237943953.pngakadu23_2-1708237971822.png

 

We have to create to 4 ui policies to acheive for all checboxes right?

1.A&B true then C&D false

2. B&C true then D& A false

3. C&D true then A&B false

4. D&A true then B&C false

abirakundu23
Mega Sage

Hi @Sachin G K1 ,

Please go ahead & test it.

1.A&B true then C&D false

2. B&C true then D& A false

3. C&D true then A&B false

4. D&A true then B&C false

Please mark Helpful & correct answer if solved your purpose.

Hi akadu,

this would work i guess, but creating 4 ui policies is suggested? is there any way to limit this?

abirakundu23
Mega Sage

Hi @Sachin G K1 ,

Instead of creating 4 ui Policy create one UI Policy & use Ui policy script logic concept according to your check box.

Below code as ref.

Var A = g_form.getvalue(u_a);

Var B = g_form.getvalue(u_b);
Var C = g_form.getvalue(u_c);

if( A == 'true' & B== 'true')

{

g_form.setReadonly('u_c', true);

}