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

Check my comments and replace the things mentioned between <>

Use this script only as i have removed many typos and other mistakes from your code.

 

 

var gr= new GlideRecord("<add actual table name>");
        gr.addQuery('u_request_item ', current.parent);
        gr.query();
        if (gr.next()) {
            if (gr.<Add actual field name>.getDisplayValue().indexOf('x')< 0) {
                gs.eventQueue("test.Notification", current, "", "");
                
            } else if (gr.<Add actual field name>.getDisplayValue().indexOf('y') < 0|| gr.<Add actual field name>.getDisplayValue().indexOf('z')< 0) {
                gs.eventQueue("test1.Notification", current, "", "");
                
            } else if (gr.<Add actual field name>.getDisplayValue().indexOf('x) < 0 && gr.<Add actual field name>.getDisplayValue().indexOf('y') < 0 || gr.<Add actual field name>.getDisplayValue().indexOf('z') < 0) {
//this will never execute as on eof the first 2 IFs will satisfy before this
                gs.eventQueue("test2.Notification", current, "", "");
                
            } else {
                gs.eventQueue("test3.Other.Notification", current, "", "");
                
            }

 

 

-Anurag

@Anurag Tripathi 

i tried all but its not working

 

Anshu_Anand_
Kilo Sage

Hi @sushma9 ,

 

Spelling mistake 3rd condition ( filed name.indexOff("x")<1 && filed name.indexOf("y")<1

"Off"

1. What is data type of field_name?

Generally if using string operations, you need to convert field values to toString()

2. If you have a ref field, you should try to get field value like

field_name.getDisplayValue() to get display value and then indexOf

3. indexOf returns -1 or index number of the string

here in your case condition should expect x,y,z to be on first index that is 0 index or not in the string

4.Also here it should be or condition. 

( filed name.indexOff("x")<1 && filed name.indexOf("y")<1

 

Hope its helpful

Regards,
Anshu

@Anshu_Anand_

1. filed type is reference and i have used the below code but its not working .

if ( filed_name.getDisplayValue().indexOf("x)<1) {

triiger the event

}else if (filed_name.getDisplayValue().indexOf("y")<1 ||filed_name.getDisplayValue().indexOf("z")<1 ){

triiger the event

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

triiger the event

} else {

triiger the event

}

Anshu_Anand_
Kilo Sage

@sushma9 

 

Lets see your code. i have commented the improvements in the code itself

 

var gr= new GlideRecord("table");
        gr.addQuery('u_request_item ', current.parent);  // is the query working? check if the query returns any value 
        gr.query();
        if (gr.next()) { 
//use alert or logs to check if the query returns any results
            if (gr.filed_name.getDisplayValue().indexOf('x')< 1) {  //if x is at 0 location that at first index, it will return true
                gs.eventQueue("test.Notification", current, "", "");
                
            } else if (gr.filed_name..getDisplayValue().indexOf('y') < 1|| gr.filed_name..getDisplayValue().indexOf('z')< 1) {
/* if x or z is at 0 location that at first index, it will return true and remove two dots before getDisplayValue() in gr.filed_name..getDisplayValue().indexOf('z') */
                gs.eventQueue("test1.Notification", current, "", "");
                
            } else if (gr.filed_name..getDisplayValue().indexOff('x) < 1 && gr.filed_name..getDisplayValue().indexOf('y') < 1|| gr.filed_name..getDisplayValue().indexOf('z') < 1) { // && condition should not be there. your code expecting x and y at same position
                gs.eventQueue("test2.Notification", current, "", "");
                
            } else {
                gs.eventQueue("test3.Other.Notification", current, "", "");
                
            }

 

Regards,
Anshu