toLowerCase is not returning the desired output on the reference type field

arundharmabe
Tera Guru

Hello SN Family,

 

I am trying to set group value based on the selected company contains specific keywords.

For Ex. If the company name contains LIC then set a group to certain value.

This is something l would like to do onChange of the company record.

 

I tried the below code it is not working but if i remove toLowerCase then it is working for LIC not for Honda because there is some issue with the toLowerCase. Can someone show some light on this.

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    var ar = (g_form.getDisplayBox('u_company').value);
       if (ar.toLowerCase().indexOf('LIC') > -1) {
        g_form.setValue('u_group', 'bc91fb081b720610cb45da01ec4bcbe6');
    }
    if (ar.toLowerCase().indexOf('Honda') > -1) {
        g_form.setValue('u_group', '5a8137481b720610cb45da01ec4bcb05');
    }
    else {
        g_form.setValue('u_group', '567133481b720610cb45da01ec4bcb45');
    }
}
 
Kind Regards,
Arun
1 ACCEPTED SOLUTION

Sandeep Rajput
Tera Patron
Tera Patron

@arundharmabe Please update your code as follows.

 

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    var ar = (g_form.getDisplayBox('u_company').value);
       if (ar.toLowerCase().indexOf('lic') > -1) {
        g_form.setValue('u_group', 'bc91fb081b720610cb45da01ec4bcbe6');
    }
    if (ar.toLowerCase().indexOf('honda') > -1) {
        g_form.setValue('u_group', '5a8137481b720610cb45da01ec4bcb05');
    }
    else {
        g_form.setValue('u_group', '567133481b720610cb45da01ec4bcb45');
    }
}

 

Reason why your code was not working: You are converting the ar variable to lower case, if it contains the keyword LIC it would get converted to lic. Similarly,  Honda would get converted to honda, here in both the conditions the companies you are looking for should also be lowercase then only the if condition will evaluate to true.

 

Hope this helps.

View solution in original post

3 REPLIES 3

Kris Moncada
Tera Guru

Try surrounding your logic with a try catch, to see what error you getting. 

 

Since toLowerCase() expects a string, I added toString() when declaring the variable ar.

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    try {
        var ar = (g_form.getDisplayBox('u_company').value).toString();
        if (ar.toLowerCase().indexOf('LIC') > -1) {
            g_form.setValue('u_group', 'bc91fb081b720610cb45da01ec4bcbe6');
        }
        if (ar.toLowerCase().indexOf('Honda') > -1) {
            g_form.setValue('u_group', '5a8137481b720610cb45da01ec4bcb05');
        } else {
            g_form.setValue('u_group', '567133481b720610cb45da01ec4bcb45');
        }

    } catch (e) {
		alert(e);
    }

}

 

I haven't tested the above code, but it should either work or provide more insight as to what is causing the issue.

Sandeep Rajput
Tera Patron
Tera Patron

@arundharmabe Please update your code as follows.

 

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    var ar = (g_form.getDisplayBox('u_company').value);
       if (ar.toLowerCase().indexOf('lic') > -1) {
        g_form.setValue('u_group', 'bc91fb081b720610cb45da01ec4bcbe6');
    }
    if (ar.toLowerCase().indexOf('honda') > -1) {
        g_form.setValue('u_group', '5a8137481b720610cb45da01ec4bcb05');
    }
    else {
        g_form.setValue('u_group', '567133481b720610cb45da01ec4bcb45');
    }
}

 

Reason why your code was not working: You are converting the ar variable to lower case, if it contains the keyword LIC it would get converted to lic. Similarly,  Honda would get converted to honda, here in both the conditions the companies you are looking for should also be lowercase then only the if condition will evaluate to true.

 

Hope this helps.

Hi Sandeep,

I further kept some alert messages and was able to fix this issue by adding else if after the 1st condition. Thank you so much for your help.

 

Kind Regards,

Arun