Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Auto populate value based on variable select variables

Krishna Priya
Tera Contributor

Auto populate value based on variable

I have two variables called sender and receiver.

In sender, choices are mail, usb, windows, land, laptop

Receiver choices are text and call.

 

If i choose sender either usb or windows, i want to populate the Receiver has text.

 

Please help me with script in catalog client script.  

12 REPLIES 12

Hi @Krishna Priya , 

use below script where i have removed that newValue =='' condition from first if condition

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading){
return;
}
if ((newValue == 'usb')||(newValue=='windows')) {
g_form.setValue('receiver', 'text');
} else {
g_form.setValue('receiver', 'call');
}
}

Hi @Krishna Priya ,

 

based on your comment, here I have assumed the initial choice of Sender is USB. When you open the form, the sender default choice will be "USB" and based on it Receiver field will be auto-populated as Text.

If user change the Sender as Laptop or Mail, receiver will auto-set as Call 

If Sender is Empty/none then receiver will also set as empty/none.

 

Follow below steps to achieve the functionality:

1. Set the default value for Sender as per your requirement.

JyotiJadhav9_0-1707374771029.png

 

2. Write the Onchange Client script for variable "Sender"

JyotiJadhav9_1-1707374954689.png

 

Script:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading) {
        if(newValue == 'usb')  
        g_form.setValue('receiver', 'text');
        //return;
    }

    //Type appropriate comment here, and begin script below
    var sender = g_form.getValue('sender');
    if (sender == 'usb' || sender == 'windows') {
        g_form.setValue('receiver', 'text');
    } else if(sender == 'mail' || sender == 'land' || sender == 'laptop') {
        g_form.setValue('receiver', 'call');
    }
    else{
        g_form.clearValue('receiver');
    }
}

 

Please hit the like button if my suggestion has helped you in any way.
Please mark correct if my response has solved your query.

 

Thanks & Regards

Jyoti Jadhav

 

Harish KM
Kilo Patron
Kilo Patron

Hi @Krishna Priya use below script:

function onChange(control, oldValue, newValue, isLoading) {
if newValue == '') {
g_form.setValue('variablename',''); // clear the value when variable is empty
}

var getSender = g_form.getValue('variablename');
if(getSender=='usb' || getSender=='windows'){
g_form.setValue('variablename','choiceValue');
}
}

Regards
Harish