Autopopulate number using options

User_267
Tera Contributor

So there are 2 fields in a catalogue item- one is Name and other is code.

Name is select box type. So it has multiple options like Ai, Dm, dj, gu, ji, kl, nv.

So when someone selects ai or DM or DJ then it needs to populate 78 number in Code field.

And if someone selects ji ,gu, kl ,then it needs to popoulate 45 number in Code field.

How to do this using client script 

2 ACCEPTED SOLUTIONS

Danish Bhairag2
Tera Sage
Tera Sage

Hi @User_267 ,

 

You need to write an onChange client script on Name field.

 

Pls try this code: use proper backend names for name & code field

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {

    if (isLoading || newValue === '') {

        return;

    }

 

    // Get the selected value from the Name field

    var nameValue = g_form.getValue('name');

 

    // Set the Code field based on the selected Name value

    if (nameValue == 'ai' || nameValue == 'AI' || nameValue == 'DM' || nameValue == 'dj') {

        g_form.setValue('code', '78');

    } else if (nameValue == 'ji' || nameValue == 'gu' || nameValue == 'kl') {

        g_form.setValue('code', '4

5');

    }

 

Thanks,

Danish

 

}

View solution in original post

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

    //Type appropriate comment here, and begin script below
    if (newValue == 'DM' || newValue == 'DJ') {
        g_form.setValue('code', '78');
    } else if (newValue == 'ji' || newValue == 'gu' || newValue == 'kl') {
        g_form.setValue('code', '45');
    }
else if (newValue == 'au' || newValue == 'DG' || newValue == 'hj') {
        g_form.setValue('code', '67');
    }


}

View solution in original post

5 REPLIES 5

Danish Bhairag2
Tera Sage
Tera Sage

Hi @User_267 ,

 

You need to write an onChange client script on Name field.

 

Pls try this code: use proper backend names for name & code field

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {

    if (isLoading || newValue === '') {

        return;

    }

 

    // Get the selected value from the Name field

    var nameValue = g_form.getValue('name');

 

    // Set the Code field based on the selected Name value

    if (nameValue == 'ai' || nameValue == 'AI' || nameValue == 'DM' || nameValue == 'dj') {

        g_form.setValue('code', '78');

    } else if (nameValue == 'ji' || nameValue == 'gu' || nameValue == 'kl') {

        g_form.setValue('code', '4

5');

    }

 

Thanks,

Danish

 

}

Sandeep Rajput
Tera Patron
Tera Patron

@User_267 Configure your script as follows.

 

Screenshot 2023-10-31 at 11.21.20 PM.png

Here is the script.

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

    //Type appropriate comment here, and begin script below
    if (newValue == 'DM' || newValue == 'DJ') {
        g_form.setValue('code', '78');
    } else if (newValue == 'ji' || newValue == 'gu' || newValue == 'kl') {
        g_form.setValue('code', '45');
    }


}

For two options it is if and else if condition if there is other options like au,DG,hj then it needs to populate 67.along with the above 

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

    //Type appropriate comment here, and begin script below
    if (newValue == 'DM' || newValue == 'DJ') {
        g_form.setValue('code', '78');
    } else if (newValue == 'ji' || newValue == 'gu' || newValue == 'kl') {
        g_form.setValue('code', '45');
    }
else if (newValue == 'au' || newValue == 'DG' || newValue == 'hj') {
        g_form.setValue('code', '67');
    }


}