How to set string field value based on choice field type

sriram7
Tera Contributor

Hi,

 

Field 1(Issue) Choice Field:  Mobile Issue, CPU Issue.

 

Field 2: based on choice selection in filed 1,  fields 2 value should populate.

 

EX: in Field 1 I have selected Mobile Issue,  in Field 2 it should populate "Describe the issue"

 

in Field 1 I have selected CPU Issue,  in Field 2 it should populate "CPU not working".

 

 

5 REPLIES 5

Ranjit Nimbalka
Mega Sage

@sriram7 

You can use Onchange Client Script on field 1.

 

onchange client script:-

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
if (newValue == 'Mobile Issue') {
g_form.setValue('field2_name', 'Describe the issue');
}
if(newValue=='CPU Issue')
{
g_form.setValue('field2_name', 'CPU not working');
}
//Type appropriate comment here, and begin script below

}
 
 
if you find this helpful please mark helpful.
 
 
Regards,
Ranjit Nimbalkar

 

Voona Rohila
Kilo Patron
Kilo Patron

Hi @sriram7 

You can write onchange client script with below logic

 

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    if (newValue == 'Mobile Issue') //map proper value here.

        g_form.setValue('field_name', 'Describe the issue'); //map proper field here.

    else if (newValue == 'CPU Issue') //map proper value here.

        g_form.setValue('field_name', 'Describe the issue'); //map proper field here.
    else
        g_form.setValue('field_name', ''); //map proper field here.

}

 

 


Mark it helpful if this helps you to understand. Accept solution if this give you the answer you're looking for
Kind Regards,
Rohila V
2022-25 ServiceNow Community MVP

Vijay kumar S1
Tera Expert

Hi Ram,

 

To set the value of a string field based on the value of a choice field in ServiceNow, you can use a client script

 

var choiceValue = g_form.getValue('Issue');

// Set the value of the string field based on the value of the choice field

if (choiceValue == 'Mobile Issue')

{

g_form.setValue('string_field', 'Field1');

}

else if (choiceValue == ' CPU Issue')

{

g_form.setValue('string_field', 'Field 2');

}

else

{

g_form.setValue('string_field', '');

}

 

You can modify based on your requirement .

 

Please mark helpful or correct if its helping 

Thank you 

Vijay

 

shruti25
Tera Contributor

Hello Sriram,
Please find the below logic.

onchange of field 1
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var issues=g_form.getValue('field_1');
if (issues == 'Mobile Issue')

g_form.setVisible('field_2', true);

else if (issues == 'CPU Issue')

g_form.setVisible('field_2', true);

}