We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

How to check string contains certain word in client script

Renu9
Tera Contributor

Hi All,

I am having location and location type variables in 1 catalog item. Based on location variable, I need to set location type variable(remote, onsite) choices in select box.

I am writing an on change client script . Requirement is if location variable contains ex: Remote, AZ or Home Office(California), then the location type should appear as Remote

Any other selection, it should be Onsite. 

But as per the below script, it is always going to Onsite . Please help me how it can be achieved

 

 var loc = g_form.getDisplayValue('location_emp');
    alert("loc is " + loc);
    if ((loc.toLowerCase().contains('remote') || loc.toLowerCase().contains('home office')) > -1) {
        alert("into if");
        g_form.setValue('location_type', 'Remote');
    } else {
        alert("into else");
        g_form.setValue('location_type', 'Onsite');
    }
}
5 REPLIES 5

Ankur Bawiskar
Tera Patron

@Renu9 

the way how display value works is different in classic and portal

update script as this; I hope your variable names are correct and you are setting correct choice values

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

    try {
        if (window == null) {
            var loc = g_form.getDisplayValue('location_emp');
            alert("loc is " + loc);
            if ((loc.toLowerCase().contains('remote') || loc.toLowerCase().contains('home office')) > -1) {
                alert("into if");
                g_form.setValue('location_type', 'Remote');
            } else {
                alert("into else");
                g_form.setValue('location_type', 'Onsite');
            }
        }
    } catch (ex) {
        var loc = g_form.getDisplayBox('location_emp').value;
        alert("loc is " + loc);
        if ((loc.toLowerCase().contains('remote') || loc.toLowerCase().contains('home office')) > -1) {
            alert("into if");
            g_form.setValue('location_type', 'Remote');
        } else {
            alert("into else");
            g_form.setValue('location_type', 'Onsite');
        }
    }
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

 

Regards,
Ankur
Certified Technical Architect  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader

Hi @Ankur Bawiskar 

I have kept the below script and it is throwing java script browser error.

 

The below info message is appearing as below

Renu9_0-1716375982647.png

 

This catalog client script is not VA supported due to the presence of the following variables in the script: window, g_form.getDisplayBox.value

@Renu9 

are you trying to use Now Assist VA Conversational catalog?

If yes then there are some limitations in it

Regards,
Ankur
Certified Technical Architect  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader

AshishKM
Kilo Patron

Hi @Renu9 , 

Use the string indexOf for checking specifix keyword within the string variable.

 

 var loc = g_form.getDisplayValue('location_emp');
    alert("loc is " + loc);
    //if ((loc.toLowerCase().contains('remote') || loc.toLowerCase().contains('home office')) > -1) {
    if(loc.indexOf("remote") != -1 || loc.indexOf("home office") != -1 ) {
        alert("into if");
        g_form.setValue('location_type', 'Remote');
    } else {
        alert("into else");
        g_form.setValue('location_type', 'Onsite');
    }
}

 

Note: add the toLower() method with loc. 

 

-Thanks,

AshishKM


Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution