Is it possible to use array in indexOf function?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-04-2023 04:20 AM - edited 09-04-2023 04:21 AM
Hi All,
I have string nm, I need to check if nm string contains any element of array of substring. I am trying to achieve it using highlighted code in attached screenshot but it is not working. If anyone implemented this type of requirement, please help me in this.
Thanks,
Tara Singh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-05-2023 01:35 AM
@Aman Kumar S I wrote code below:-
(function executeRule(current, previous) {
var nm =current.getValue('name'); // e.g. This is a high school. means main string
var desc =current.getValue('description');
var tArray=[];
var tWord= new GlideRecord('sn_risk_advanced_trending_word');
tWord.addEncodedQuery('u_active=true'); // At this line ,if we filter specific one keyword then code will work easily.then no need to do extra stuff. e.g high school
tWord.query();
while(tWord.next())
{
tArray.push(tWord.getValue('u_trending_word')); // tArray values would be ["high school","college"] means substrs
for(var i=0;i<tArray.length;t++)
{
if(nm.indexOf(tArray[i]) > -1 || desc.indexOf(tArray[i])>-1)
{
tWord.tArray[i].setValue('statu', "2"); //Here I am also struggling to set value.
}
}
tWord.update();
}
}(current, previous)

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-05-2023 03:58 AM
What is your objective of writing your code, please explain the use case, the code is not self explanatory I am afraid.
What is being stored in tArray? Why are you even storing value in array?
What value you trying to set and why?
Aman Kumar

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-04-2023 11:19 AM
@Tarasingh26 I have used the string.indexOf function and it worked fine for me. The only change I did in my script is to convert both the string values (which need to be compared to lower case to find a correct match.)
Following script worked for me.
var nm = 'Rock Paper Scissor';
nm = nm.toLowerCase();
var someArray = ['some','rock','hair'];
for(var i=0;i<someArray.length;i++){
if(nm.indexOf(someArray[i].toLowerCase())>-1){
gs.info('found '+someArray[i]);
}
else{
gs.info(' not found '+someArray[i]);
}
}
Hope this helps.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-05-2023 01:37 AM
@Sandeep Rajput Hi Sandeep , Thanks for reply , I am using in asyn BR but it not worked. Below is code can you please check?
(function executeRule(current, previous) {
var nm =current.getValue('name'); // e.g. This is a high school. means main string
var desc =current.getValue('description');
var tArray=[];
var tWord= new GlideRecord('sn_risk_advanced_trending_word');
tWord.addEncodedQuery('u_active=true'); // At this line ,if we filter specific one keyword then code will work easily.then no need to do extra stuff. e.g high school
tWord.query();
while(tWord.next())
{
tArray.push(tWord.getValue('u_trending_word')); // tArray values would be ["high school","college"] means substrs
for(var i=0;i<tArray.length;t++)
{
if(nm.indexOf(tArray[i]) > -1 || desc.indexOf(tArray[i])>-1)
{
tWord.tArray[i].setValue('statu', "2"); //Here I am also struggling to set value.
}
}
tWord.update();
}
}(current, previous)