Field contains and does not contains a value

Ankita9793
Tera Contributor

Hi All,
I am not sure what is wrong with the below script, i'm trying to compare field value where it contains and does not contains (1Global). Background script executes both the conditions, please suggest.

 

 

var newcar = 'Truphone';

var oldcar = '1Global (UK)';

var porting = 'Yes';

 

if (porting == 'Yes') {

    if ((newcar.toString().indexOf('1Global') == -1) && (oldcar.toString().indexof('1Global') != -1)) { //Does not contain(anything apart from 1Global) and contain 1Global

        gs.print('add' + porting + '>>' + newcar + '>>' + oldcar);

        gs.print(+(newcar.toString().indexOf('1Global')) + '>>' + (oldcar.toString().indexOf('1Global')));

    }

 gs.print(+(newcar.toString().indexOf('1Global')) + '>>' + (oldcar.toString().indexOf('1Global')));

    else if (((newcar.toString().indexOf('1Global')) !== -1) && (oldcar.toString().indexof('1Global') === -1)); {

         //contain 1Global and Does not contain(anything apart from 1Global)

         gs.print('remove' + porting + '>>' + newcar + '>>' + oldcar);

        gs.print(+(newcar.toString().indexOf('1Global')) + '>>' + (oldcar.toString().indexOf('1Global')));

    }

}

5 REPLIES 5

Bhavya11
Kilo Patron
Kilo Patron

Hi @Ankita9793 ,

 

can you remove the semicolon from ' else if (((newcar.toString().indexOf('1Global')) !== -1) && (oldcar.toString().indexof('1Global') === -1));' this line and change indexof to indexOf

 

try to run this script:

var newcar = 'Truphone';
var oldcar = '1Global (UK)';
var porting = 'Yes';

 

if (porting == 'Yes') {

    if ((newcar.toString().indexOf('1Global') == -1) && (oldcar.toString().indexOf('1Global') != -1)) { //Does not contain(anything apart from 1Global) and contain 1Global

        gs.print('add' + porting + '>>' + newcar + '>>' + oldcar);

        gs.print(+(newcar.toString().indexOf('1Global')) + '>>' + (oldcar.toString().indexOf('1Global')));

    }


    else if (((newcar.toString().indexOf('1Global')) !== -1) && (oldcar.toString().indexOf('1Global') === -1)) {

         //contain 1Global and Does not contain(anything apart from 1Global)

         gs.print('remove' + porting + '>>' + newcar + '>>' + oldcar);

        gs.print(+(newcar.toString().indexOf('1Global')) + '>>' + (oldcar.toString().indexOf('1Global')));

    }

}

 

 

If this information proves useful, kindly mark it as helpful or accepted solution.

 

Thanks,

BK

Me Being Mustaq
Tera Guru

Hi @Ankita9793 ,,

 

Two separate issues cause this behavior  typos in indexOf and a stray semicolon after else if, which makes the second block run unconditionally

  • JavaScript is case-sensitive, so oldcar.toString().indexof('1Global') is undefined and will throw an error or behave unexpectedly.

  • It must be indexOf everywhere

Semicolon after else if:

Any statement between a closing if block and an else if breaks the chain and makes the else if invalid JavaScript

 

Corrected Script:-

var newcar  = 'Truphone';
var oldcar  = '1Global (UK)';
var porting = 'Yes';

if (porting == 'Yes') {
    var newHas  = newcar.toString().indexOf('1Global');
    var oldHas  = oldcar.toString().indexOf('1Global');

    // Does NOT contain 1Global (new) and DOES contain 1Global (old)
    if (newHas == -1 && oldHas != -1) {
        gs.print('add ' + porting + ' >> ' + newcar + ' >> ' + oldcar);
        gs.print(newHas + ' >> ' + oldHas);
    }
    // DOES contain 1Global (new) and does NOT contain 1Global (old)
    else if (newHas != -1 && oldHas == -1) {
        gs.print('remove ' + porting + ' >> ' + newcar + ' >> ' + oldcar);
        gs.print(newHas + ' >> ' + oldHas);
    }
}

 

If it is helpful, please hit the thumbs button and accept the correct solution by referring to this solution in the future it will be helpful to them.

 

 

Thanks & Regards,

Mohammed Mustaq Shaik

 

 

Ankur Bawiskar
Tera Patron
Tera Patron

@Ankita9793 

try this

var newcar = 'Truphone';
var oldcar = '1Global (UK)';
var porting = 'Yes';

if (porting === 'Yes') {
    var has1GlobalNew = newcar.indexOf('1Global') !== -1;
    var has1GlobalOld = oldcar.indexOf('1Global') !== -1;

    if (!has1GlobalNew && has1GlobalOld) {
        // New does NOT contain 1Global, Old DOES contain 1Global
        gs.print('add ' + porting + ' >> ' + newcar + ' >> ' + oldcar);
        gs.print('!' + has1GlobalNew + ' && ' + has1GlobalOld);
    } else if (has1GlobalNew && !has1GlobalOld) {
        // New DOES contain 1Global, Old does NOT contain 1Global
        gs.print('remove ' + porting + ' >> ' + newcar + ' >> ' + oldcar);
        gs.print(has1GlobalNew + ' && !' + has1GlobalOld);
    }
}

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

@Ankita9793 

Thank you for marking my response as helpful.

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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