Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Index of Not working

sushma9
Tera Contributor

Hi All,

I am using Index of  with string and its not working. Please find the sample script below. when i tested i am only getting the If validation and else if is not working. please let  me know if any changes has to done.

Note : filed name is same for all the conditions.

script :

 if (field name.indexOf("x")<1){

here i am triggering one event 

} else if (filed name.indexOf("y")<1 || filed name.indexOf("z")<1){

here i am triggering another event 

} else if ( filed name.indexOff("x")<1 && filed name.indexOf("y")<1 || filed name.indexOf("z")<1){

here i am triggering another event  ;

}else{

here i am triggering another event  ;

}

14 REPLIES 14

Rahul Talreja
Mega Sage

Hi @sushma9 ,

Seems like confusion in logic.
Try below code if it serves your purpose.

if (fieldname.indexOf("x") === -1) {
    // here i am triggering one event 
} else if (fieldname.indexOf("y") === -1 || fieldname.indexOf("z") === -1) {
    // here i am triggering another event 
} else if (fieldname.indexOf("x") === -1 && (fieldname.indexOf("y") === -1 || fieldname.indexOf("z") === -1)) {
    // here i am triggering another event
} else {
    // here i am triggering another event
}


Let me know if you are looking for something else.

Please mark my response correct/helpful as applicable!
Thanks and Regards,
Rahul

@Rahul Talreja 

Its not working rahul

@sushma9 
Try with this:

var gr= new GlideRecord("table");
gr.addQuery('u_request_item ', current.parent);
gr.query();
if (gr.next()) {
    if (gr.field_name.getDisplayValue().indexOf('x')< 1) {
        gs.eventQueue("test.Notification", current, "", "");
        
    } else if (gr.field_name.getDisplayValue().indexOf('y') < 1 || gr.field_name.getDisplayValue().indexOf('z')< 1) {
        gs.eventQueue("test1.Notification", current, "", "");
        
    } else if (gr.field_name.getDisplayValue().indexOf('x') < 1 && gr.field_name.getDisplayValue().indexOf('y') < 1 || gr.field_name.getDisplayValue().indexOf('z') < 1) {
        gs.eventQueue("test2.Notification", current, "", "");
        
    } else {
        gs.eventQueue("test3.Other.Notification", current, "", "");
        
    }
}


Can you please confirm of your desired result? What you expect, as the script is correct in syntax part buy there is ambiguity in logic part. Your if-else conditions seem to be overlapping which might not provide the desired result.

Please mark my response correct/helpful as applicable!
Thanks and Regards,
Rahul

Anurag Tripathi
Mega Patron
Mega Patron

indexOf returns -1 when the string is not found, so your logic to check <1 will not work if the string you look for is the first character, they this

Also what is field name??

 

 

//what is field name?  

if (field name.indexOf("x")<0){
//here i am triggering one event 
} else if (field name.indexOf("y")<0 || field name.indexOf("z")<0){
//here i am triggering another event 
} else if ( field name.indexOf("x")<0 && field name.indexOf("y")<0 || field name.indexOf("z")<0){  //this will never run as it will go in one of the first 2 if 
//here i am triggering another event  ;
}else{
//here i am triggering another event  ;
}

 

 

 

 

-Anurag