Restriction of Choices list

Gautam Raj
Tera Contributor

Can we restrict a choice list to a specific entry alone? 

Am having a business application with cloud type as IaaS. I need to add an additional choice (CSP) under the choices tab in dictionary entry and restrict it to only one business application. Is that possible.

 

For example in the below BA could type should be marked as CSP and that choice should only be restricted to this BA. is that possible ?

GautamRaj_0-1738056520807.png

 

2 ACCEPTED SOLUTIONS

@Gautam Raj 

then do this

1) use list_edit ACL and block the edit operation

2) Now for form level do this (client script on table cmdb_ci_business_app)

a) create onLoad client script and check if the name is Infra-Cloud-Azure BA or Infra - Cloud BA, if yes then set the choice value and make readonly

b) in same onLoad if value is not that then remove that option from choice

function onLoad() {
    var name = g_form.getValue('name');
    if (name == 'Infra-Cloud-Azure BA' || name == 'Infra - Cloud BA') {
        g_form.setValue('u_cloud_type', 'CSP')
        g_form.setReadOnly('u_cloud_type', true);
    } else {
        g_form.removeOption('u_cloud_type', 'CSP');
    }
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

@Gautam Raj 

the value in choice is csp and not CSP

so you need to give csp when you use removeOption

here is the updated code

function onLoad() {
    var name = g_form.getValue('name');
    if (name == 'Infra-Cloud-Azure BA' || name == 'Infra - Cloud BA') {
        g_form.setValue('u_cloud_type', 'csp')
        g_form.setReadOnly('u_cloud_type', true);
    } else {
        g_form.removeOption('u_cloud_type', 'csp');
    }
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

21 REPLIES 21

@Gautam Raj 

in list view you cannot hide the option

2 ways to handle this

1) use before update BR and see if logged in user Doesn't belongs to group "Infra-Cloud-Azure BA" and Cloud Type [CHANGES TO] CSP -> then abort the BR

OR

2) don't allow list edit and it will stop the form update

Remember the 1st approach will work on both list and form view.

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Gautam Raj 

what's your requirement?

you want to stop user from selecting that option

OR
you want the choice value to be set with that option if the value is Infra-Cloud-Azure BA

Please mention the correct business requirement so that I can guide you.

 

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Community Alums
Not applicable

Hello,

 

you can write a client script for this and hide the other choices (using removeOption) when BA name is Infra-Cloud-Azure BA

Runjay Patel
Giga Sage

HI @Gautam Raj ,

 

You can try below script.

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

    // Assuming "Business Application" field is on the form
    var baSysId = g_form.getValue('business_application'); // Replace with actual field name
    var restrictedBA = 'sys_id_of_business_application'; // Replace with the sys_id of the specific BA
    
    // Check if the current Business Application is the restricted one
    if (baSysId == restrictedBA) {
        // If it's the specific Business Application, only allow "CSP" in Cloud Type
        g_form.setChoice('cloud_type', 'CSP');
        g_form.setReadOnly('cloud_type', true); // Make it read-only if necessary
    } else {
        // Otherwise, allow all choices
        g_form.setReadOnly('cloud_type', false);
    }
}

 

-------------------------------------------------------------------------

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

-------------------------------------------------------------------------

Hi @Runjay Patel Thanks for your response.

Below is the field value in the cmdb_ci_business_app

GautamRaj_0-1738064792665.png

As per your suggestion i have created a client script as below but its not changing can you review the below code 

GautamRaj_1-1738064943569.png

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }
    var baSysId = g_form.getValue('name');
    var restrictedBA = '44b1c5f6875d1158aa2fa6890cbb35c1';

    if (baSysId == restrictedBA) {
        g_form.setChoice('u_cloud_type', 'CSP');
        g_form.setReadOnly('u_cloud_type', true);
    } else {
        g_form.setReadOnly('u_cloud_type', false);
    }
}