Index of Not working
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-29-2023 02:38 AM - edited 11-29-2023 02:40 AM
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 ;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-29-2023 02:51 AM
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.
Thanks and Regards,
Rahul
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-29-2023 03:40 AM
Its not working rahul
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-29-2023 05:59 AM
@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.
Thanks and Regards,
Rahul
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-29-2023 02:53 AM - edited 11-29-2023 02:54 AM
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 ;
}