- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-03-2022 11:39 PM
I would like to use multiple "indexOf" in a field map script.
I have created multiple conditions using "if" and "else if".
The first "indexOf" works, but the second and subsequent "indexOf" don't work.
Do you know how to deal with this?
Or if you have an example, I would appreciate it if you could describe it.
Thank you in advance.
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-07-2022 12:46 AM
Try as below:
answer = (function transformEntry(source) {
var result = [];
if (source.fieldnameA.indexOf('x') > -1) {
result.push("xxxxx");
if (source.fieldnameB.indexOf('y') > -1) {
result.push("yyyyy");
if (source.fieldnameC.indexOf('z') > -1) {
result.push("zzzzz");
}
return result;
})(source);
Aman Kumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-03-2022 11:51 PM
Hello Daiki,
Can you just post your script here
Thanks,
Ajay
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-07-2022 12:39 AM
Hello Ajay
It is described this way.
In this case, only "x" returns, but "y" and "z" do not.
answer = (function transformEntry(source) {
if (source.fieldnameA.indexOf('x') > -1) {
return "xxxxx";
} else if (source.fieldnameB.indexOf('y') > -1) {
return "yyyyy";
} else if (source.fieldnameC.indexOf('z') > -1) {
return "zzzzz";
}
})(source);
Thank you in advance.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-07-2022 12:46 AM
Try as below:
answer = (function transformEntry(source) {
var result = [];
if (source.fieldnameA.indexOf('x') > -1) {
result.push("xxxxx");
if (source.fieldnameB.indexOf('y') > -1) {
result.push("yyyyy");
if (source.fieldnameC.indexOf('z') > -1) {
result.push("zzzzz");
}
return result;
})(source);
Aman Kumar

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-04-2022 12:06 AM
Hi,
There are many ways to solve this, if you could describe your use case a bit more you can more adequate help.
One simple way to solve it, is to store the text you want to find in variables, and later use those variables in if-conditions or a switch-case statement.
var test = 'My test string with many words in it';
var stringExist = test.indexOf('string');
var helloExist = test.indexOf('hello');
var inExist = test.indexOf('in');
gs.info('string: ' + stringExist);
gs.info('hello: ' + helloExist);
gs.info('in: ' + inExist);
/* Output:
*** Script: string: 8
*** Script: hello: -1
*** Script: in: 11
*/