Autopopulate Select box variable based on string variable

JRY
Mega Guru

Hello All,

 

I'm trying to populate a selectbox variable based on a single line text field. As we can see in the below image, I'm trying to autopopulate dropdown choice based on a single line text field value. As you can see in the below image, but below script, it's not working as expected. Can someone help me with corrections to the script?

 

LAN ID == Single line Text variable

Domain == Dropdown/selectbo

JRY_0-1666710521861.png

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var lanId = g_form.getValue('lan_id');
    lanId = ["ACCPT", "AF", "AP", "EA", "NA", "SA", "UPSTREAMACCTS"];
    if (lanId.indexOf('ACCPT') !== -1) {
        g_form.setValue('domain', 'ACCPT');
    } else if (lanId.indexOf('AF') !== -1) {
        g_form.setValue('domain', 'AF');
    } else if (lanId.indexOf('AP') !== -1) {
        g_form.setValue('domain', 'AP');
    } else if (lanId.indexOf('EA') !== -1) {
        g_form.setValue('domain', 'EA');
    } else if (lanId.indexOf('NA') !== -1) {
        g_form.setValue('domain', 'NA');
    } else if (lanId.indexOf('SA') !== -1) {
        g_form.setValue('domain', 'SA');
    } else if (lanId.indexOf('UPSTREAMACCTS') !== -1) {
        g_form.setValue('domain', 'UPSTREAMACCTS');
    }


}

 

Thanks,

JRY

 

 

 

1 ACCEPTED SOLUTION

Murthy Ch
Giga Sage

Hi @JRY 

Can you try this:

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var lanId = g_form.getValue('lan_id');
    //lanId = ["ACCPT", "AF", "AP", "EA", "NA", "SA", "UPSTREAMACCTS"]; //commented this line
    if (lanId.indexOf('ACCPT') >-1) {
        g_form.setValue('domain', 'ACCPT');
    } else if (lanId.indexOf('AF') >-1) {
        g_form.setValue('domain', 'AF');
    } else if (lanId.indexOf('AP') >-1) {
        g_form.setValue('domain', 'AP');
    } else if (lanId.indexOf('EA')>-1) {
        g_form.setValue('domain', 'EA');
    } else if (lanId.indexOf('NA') >-1) {
        g_form.setValue('domain', 'NA');
    } else if (lanId.indexOf('SA') >-1) {
        g_form.setValue('domain', 'SA');
    } else if (lanId.indexOf('UPSTREAMACCTS') >-1) {
        g_form.setValue('domain', 'UPSTREAMACCTS');
    }


}

 

Hope it works

 

Thanks,
Murthy

View solution in original post

10 REPLIES 10

Hi Murthy,

 

I have tried your script as you can see in below image it's taking wrong choice

JRY_0-1666771185332.png

 

Hi @JRY 

Yes it's because it is satisfying the below else if condition before going to the last else if

 else if (lanId.indexOf('EA')>-1) {
        g_form.setValue('domain', 'EA');

As your text contains the EA in the LAN ID the EA option is populated.

So you just need to update the condition in the else if where it has to be unique in the indexOf

 

Hope it helps..

Note: No issues in the logic.

 

 

 

 

Thanks,
Murthy

Thanks, I have modified the value now it's working fine.

 

Thanks,

JRY 

Saurav11
Kilo Patron
Kilo Patron

Hello,

 

Are you trying to set a single value or a choice of values in the dropdown:-

 

If it is a choice of value add all the options at top i.e by using :-

 

g_form.addOption('domain','ACCPT','ACCPT',1); // do it for all options

 

and then in the if statement just remove the option using:-

 

g_form.removeOption('domain','ACCPT');

 

Please mark my answer as correct based on Impact.

 

Hi Saurav,

I tried the below script as you suggested, but it didn't work. capturing the wrong value. Can you please tell me what went wrong with the below script?

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        g_form.addOption('domain', 'ACCPT', 'ACCPT', 1);
        g_form.addOption('domain', 'AF', 'AF', 2);
        g_form.addOption('domain', 'AP', 'AP', 3);
        g_form.addOption('domain', 'EA', 'EA', 4);
        g_form.addOption('domain', 'NA', 'NA', 5);
        g_form.addOption('domain', 'SA', 'SA', 6);
        g_form.addOption('domain', 'UPSTREAMACCTS', 'UPSTREAMACCTS', 7);
        return;
    }
    var lanId = g_form.getValue('lan_id');
    lanId = ["ACCPT", "AF", "AP", "EA", "NA", "SA", "UPSTREAMACCTS"];
    if (lanId.indexOf('ACCPT')) {
        g_form.addOption('domain', 'ACCPT', 'ACCPT');
        g_form.removeOption('domain', 'AF');
        g_form.removeOption('domain', 'AP');
        g_form.removeOption('domain', 'EA');
        g_form.removeOption('domain', 'NA');
        g_form.removeOption('domain', 'SA');
        g_form.removeOption('domain', 'UPSTREAMACCTS');
    } else if (lanId.indexOf('AF')) {
        g_form.addOption('domain', 'AF', 'AF');
        g_form.removeOption('domain', 'ACCPT');
        g_form.removeOption('domain', 'AP');
        g_form.removeOption('domain', 'EA');
        g_form.removeOption('domain', 'NA');
        g_form.removeOption('domain', 'SA');
        g_form.removeOption('domain', 'UPSTREAMACCTS');
    } else if (lanId.indexOf('AP')) {
        g_form.addOption('domain', 3, 'AP');
        g_form.removeOption('domain', 'ACCPT');
        g_form.removeOption('domain', 'AF');
        g_form.removeOption('domain', 'EA');
        g_form.removeOption('domain', 'NA');
        g_form.removeOption('domain', 'SA');
        g_form.removeOption('domain', 'UPSTREAMACCTS');
    } else if (lanId.indexOf('EA')) {
        g_form.addOption('domain', 'EA', 'EA');
        g_form.removeOption('domain', 'ACCPT');
        g_form.removeOption('domain', 'AF');
        g_form.removeOption('domain', 'AP');
        g_form.removeOption('domain', 'NA');
        g_form.removeOption('domain', 'SA');
        g_form.removeOption('domain', 'UPSTREAMACCTS');
    } else if (lanId.indexOf('NA')) {
        g_form.addOption('domain', 'NA', 'NA');
        g_form.removeOption('domain', 'ACCPT');
        g_form.removeOption('domain', 'AF');
        g_form.removeOption('domain', 'AP');
        g_form.removeOption('domain', 'EA');
        g_form.removeOption('domain', 'SA');
        g_form.removeOption('domain', 'UPSTREAMACCTS');
    } else if (lanId.indexOf('SA')) {
        g_form.addOption('domain', 'SA', 'SA');
        g_form.removeOption('domain', 'ACCPT');
        g_form.removeOption('domain', 'AF');
        g_form.removeOption('domain', 'AP');
        g_form.removeOption('domain', 'NA');
        g_form.removeOption('domain', 'EA');
        g_form.removeOption('domain', 'UPSTREAMACCTS');
    } else if (lanId.indexOf('UPSTREAMACCTS')) {
        g_form.addOption('domain', 'UPSTREAMACCTS', 'UPSTREAMACCTS');
        g_form.removeOption('domain', 'ACCPT');
        g_form.removeOption('domain', 'AF');
        g_form.removeOption('domain', 'AP');
        g_form.removeOption('domain', 'NA');
        g_form.removeOption('domain', 'SA');
        g_form.removeOption('domain', 'EA');
    }


}