I would like to use multiple "indexOf" in a field map script.

Daiki Shinohara
Tera Contributor

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.

1 ACCEPTED SOLUTION

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);

Best Regards
Aman Kumar

View solution in original post

6 REPLIES 6

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

ensure you use toString() on the value you want to search before using indexOf

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Vishnu Prasad K
Giga Guru

Hi Daiki,

 

Try using switch case as you have multiple else if. But still you need to check why it's not working in a different conditions, you can add log statement in each else if.