if condition js

levino
Giga Guru

Hi there

the below script keeps evaluating to FRUNIX output, even if value is 'pwdlmos01' in the unix_server_name field

unix_server_name is a text field.

 

Thanks

Levino

 

 

 

 

function onSubmit() {
    //Type appropriate comment here, and begin script below
    var access = g_form.getValue('unix_server_name');

    if (access == 'pwdlmos01' || 'paplmos01') {
        g_form.setValue('request_template', '2LS OSS');
    } else if (access != 'pwdlmos01' || 'paplmos01') {
        g_form.setValue('request_template', 'FRUNIX');

    }

}

 

 

1 ACCEPTED SOLUTION

Deepak Shaerma
Kilo Sage

Hi @levino 
The issue you’re encountering stems from a common misunderstanding of how logical comparisons work in JavaScript.
In your if condition:
if (access == ‘pwdlmos01’ || ‘paplmos01’) 
The 'paplmos01' is always evaluated as true because non-empty strings are truthy in JavaScript, making the condition always true, and thus the code always sets the value to '2LS OSS'.

Similarly, in your else if condition:
else if (access != ‘pwdlmos01’ || ‘paplmos01’) 
This suffers from the same issue, where 'paplmos01' is considered true, but due to the nature of how you’ve structured your conditions, this block wouldn’t be executed based on your described behavior.

function onSubmit() {
    //Type appropriate comment here, and begin script below
    var access = g_form.getValue('unix_server_name');

    if (access == 'pwdlmos01' || access == 'paplmos01') {
        g_form.setValue('request_template', '2LS OSS');
    } else {
        g_form.setValue('request_template', 'FRUNIX');
    }
}

Please Mark this Helpful and Accepted Solution. If this Helps you to understand. This will help both the community and me..
- Keep Learning ‌‌
Thanks & Regards 
Deepak Sharma 

View solution in original post

1 REPLY 1

Deepak Shaerma
Kilo Sage

Hi @levino 
The issue you’re encountering stems from a common misunderstanding of how logical comparisons work in JavaScript.
In your if condition:
if (access == ‘pwdlmos01’ || ‘paplmos01’) 
The 'paplmos01' is always evaluated as true because non-empty strings are truthy in JavaScript, making the condition always true, and thus the code always sets the value to '2LS OSS'.

Similarly, in your else if condition:
else if (access != ‘pwdlmos01’ || ‘paplmos01’) 
This suffers from the same issue, where 'paplmos01' is considered true, but due to the nature of how you’ve structured your conditions, this block wouldn’t be executed based on your described behavior.

function onSubmit() {
    //Type appropriate comment here, and begin script below
    var access = g_form.getValue('unix_server_name');

    if (access == 'pwdlmos01' || access == 'paplmos01') {
        g_form.setValue('request_template', '2LS OSS');
    } else {
        g_form.setValue('request_template', 'FRUNIX');
    }
}

Please Mark this Helpful and Accepted Solution. If this Helps you to understand. This will help both the community and me..
- Keep Learning ‌‌
Thanks & Regards 
Deepak Sharma