Some PDIs are currently unavailable, and PDI actions are paused. View the latest updates here. Read More

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

    }

}

6 REPLIES 6

@Ankita9793 

Thank you for marking my response as helpful.

Just following up to see whether my suggested solution worked for you.

💡 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  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader

Sarthak Kashyap
Mega Sage

Hi @Ankita9793 ,

 

I saw 2 issues in your code 

1. You put the semicolon after else if condition 

2. indexof is replaced with indexOf

Check below code 

 

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

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

 

SarthakKashyap_0-1767595860823.png

 

 

Please mark my answer correct and helpful if this works for you

Thanks and Regards,

Sarthak