I need to use startsWith() function on a client script, but I got an error

SN Emy
Tera Guru

Hi,

I need to use startsWith() function on a client script, but I got this error.

           onChange script error: TypeError: location.startsWith is not a function function () { [native code] }

 

please help?

 

1 ACCEPTED SOLUTION

SN Emy
Tera Guru

I fixed it, I have to add .name to the location.

Than you all.

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var location = g_form.getReference('u_fac_building');
//g_form.addInfoMessage(location.name);

if(location.name.substring(0,4) == "0152"){

g_form.setValue('assigned_to','442d90fa4f4f8b003aeb58211310c784');


}
}

View solution in original post

13 REPLIES 13

Did you try this code?

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var location = g_form.getDisplayBox('u_fac_building');


if(location.startsWith("0152")){

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

}


}


Please mark this response as correct or helpful if it assisted you with your question.

djohnson1
Mega Guru

SN Emy, 

      Can you provide more details into your requirement? I instinctively want to suggest the indexOf() function rather than startsWith() in the client. 

 

Thanks, 

Derrick Johnson

SN Emy
Tera Guru

below is my script:

 

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


var location = g_form.getReference('u_fac_building');'

 


if(location.startsWith("0152")){

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



}


}

SN Emy, 

        Because this is an On-Change client script, we can simplify the script like so:

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

    //Type appropriate comment here, and begin script below
    if (newValue.startsWith('0152')) {
        g_form.setValue('assigned_to','');
    }
}

       Basically, this script clears the 'assigned_to' value when the value of the location field starts with 0152. We do not need to assign the value of u_fac_building to a variable because the script will be listening for changes to this field while the form is loaded. 

 

Thanks, 

Derrick Johnson

Since location is a reference field, you can try

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var location = g_form.getDisplayBox('u_fac_building');


if(location.startsWith("0152")){

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

}


}


Please mark this response as correct or helpful if it assisted you with your question.