Client script to default a value

Jen11
Tera Expert

Hello All,

I have a select box field, when the option Temporary is selected:

Jen11_0-1697217155206.png

I have another select box below this, I would like 'All Department' to default and make this field read only.

Jen11_1-1697217217860.png

How can i go about doing this? 

1 ACCEPTED SOLUTION

Hemanth M1
Giga Sage
Giga Sage

Hi @Jen11 , write a onChange catalog client scrpt as below:

HemanthM1_0-1697219906084.png

 

script : 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    //Type appropriate comment here, and begin script below

    if (newValue == "abc") { //replace bac with Temporary choice  value
        g_form.setValue("s2", "pqr"); //replace s2 - select box 2 variable name and PQR with All Department' value
        g_form.setReadOnly("s2", true); //replace s2 with select box varibale name
    }



}

 

 

Accept and hit Helpful if it helps.

Thank you,
Hemanth
Certified Technical Architect (CTA), ServiceNow MVP 2024, 2025

View solution in original post

6 REPLIES 6

Sandeep Rajput
Tera Patron
Tera Patron

@Jen11 Please add a UI Policy on your catalog item as follows.

Screenshot 2023-10-13 at 11.18.56 PM.pngScreenshot 2023-10-13 at 11.19.17 PM.pngScreenshot 2023-10-13 at 11.19.48 PM.png

This is how the end result look

Screenshot 2023-10-13 at 11.21.31 PM.png

Hope this helps.

Hemanth M1
Giga Sage
Giga Sage

Hi @Jen11 , write a onChange catalog client scrpt as below:

HemanthM1_0-1697219906084.png

 

script : 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    //Type appropriate comment here, and begin script below

    if (newValue == "abc") { //replace bac with Temporary choice  value
        g_form.setValue("s2", "pqr"); //replace s2 - select box 2 variable name and PQR with All Department' value
        g_form.setReadOnly("s2", true); //replace s2 with select box varibale name
    }



}

 

 

Accept and hit Helpful if it helps.

Thank you,
Hemanth
Certified Technical Architect (CTA), ServiceNow MVP 2024, 2025

Thank you Hermanth! 

@Hemanth so that's working, but when i go select Temporary, all department is set to default, when i change to something else beside temporary, all department doesn't change unless i refresh the page.  

 

Jen11_0-1697225764023.png