I want to get userlocationcity by using indexof() function

sainath reddy
Tera Contributor

i'm using userlocationcity variable from location to get the city.after getting the location i have to use 'if' condition with the indexOf() function with the userlocationcity.indexOf("string").how to search the string by using indexOf() > 1 to match the given string in indexOf() string to the userlocation string

var userlocationcity = current.variables.requested_for.location.toString().toLowerCase();

if((userlocationcity == 'hyderabad')||(userlocation.indexOf('hyderabad')==-1)){

}

2 ACCEPTED SOLUTIONS

Prince Arora
Tera Sage
Tera Sage

@sainath reddy 

 

Basically "indexOf" is the Javascript String function it returns 

"-1" - when string is not found

 

Index - returns the index of string where it is available in the complete sentence

 

you can check as below method:

 

if(userlocationcity.indexOf(String) !=-1){ // it means its found

}

 

OR

 

if(userlocationcity.indexOf(String) >= 0 ){
}

 

If my answer solved your issue, please mark my answer as  Correct & 👍Helpful based on the Impact.

View solution in original post

Chetan Mahajan
Kilo Sage
Kilo Sage

Hello @sainath reddy ,

If you want to check if a specific string is present anywhere in the userlocationcity string, you should use indexOf() != -1. This condition will return true if the substring is found and false if it's not found. use script as below

 

 

// Assume userlocationcity contains the user's location city as a string
var userlocationcity = "Santa Rosa, CA USA";

// String to search for
var searchString = "Santa Rosa";

// Check if the searchString is present in userlocationcity using indexOf()
if (userlocationcity.indexOf(searchString) != -1) {
  // The searchString is found in userlocationcity
  gs.info("Search string found in user's location city.");
} else {
  // The searchString is not found in userlocationcity
  gs.info("Search string not found in user's location city.");
}

 

 

Kindly mark Correct and helpful if applicable

View solution in original post

6 REPLIES 6

var userlocationcity = current.variables.requested_for.location.toString().toLowerCase();
 
 
if(userlocationcity.indexOf("hyd") != -1){
current.assignment_group = gs.getProperty('Assignment group based on Hyd');
}
else if(userlocationcity.indexOf("mum") != -1){
current.assignment_group = gs.getProperty('Assignment group based on MUMBAI');
}
else {
current.assignment_group = gs.getProperty('Service Desk');
}
 
here in indexOf("string") i have given city name in workflow run script.its not working
 

@sainath reddy 

 

var userlocationcity = current.variables.requested_for.location.toString().toLowerCase();

 

Can you print "userlocationcity", I hope you will get the sys_id , if so you need to do something like this.

 

var userlocationcity = (current.variables.requested_for.location.)getDisplayValue();