Field contains and does not contains a value
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Sunday
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')));
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Sunday - last edited Sunday
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Sunday
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Sunday
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! 🙏
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
yesterday
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! 🙏
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
