switch case in servicenow script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-29-2022 11:07 AM
What's the deal with Switch case logic in servicenow script? my switch logic was working fine client script but same switch case didn't work in script include.
Below code worked fine with if-else but not with switch case -_-
I did some search on community before posting this question. I used for ' ' quote and " " double quote. Tried .toString() and some other things. Any advice would be helpful. What's frustrating is same switch case works fine in client script side.
var categoryName = this.getParameter('sysparm_categoryName').toString();
var assignDetail = {};
if (categoryName == 'inquiry') {
assignDetail.groupSysID = '679434f053231300e321ddeeff7b12d8';
}else if(categoryName == 'hardware'){
assignDetail.groupSysID = '8a5055c9c61122780043563ef53438e3';
}else if(categoryName == 'software'){
assignDetail.groupSysID = '8a4dde73c6112278017a6a4baf547aa7';
}else if(categoryName == 'network'){
assignDetail.groupSysID = '287ebd7da9fe198100f92cc8d1d2154e';
}else if(categoryName == 'database'){
assignDetail.groupSysID = '287ee6fea9fe198100ada7950d0b1b73';
}
// switch (categoryName) {
// case "inquiry":
// assignDetail.groupSysID = '679434f053231300e321ddeeff7b12d8';
// gs.addInfoMessage(categoryName);
// break;
// case "hardware":
// assignDetail.groupSysID = '8a5055c9c61122780043563ef53438e3';
// break;
// case "software":
// assignDetail.groupSysID = '8a4dde73c6112278017a6a4baf547aa7';
// break;
// case "network":
// assignDetail.groupSysID = '287ebd7da9fe198100f92cc8d1d2154e';
// break;
// case "database":
// assignDetail.groupSysID = '287ee6fea9fe198100ada7950d0b1b73';
// break;
// }
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-02-2022 11:12 PM
Yeah this way it;'s working on my system as well. But when I pass value from client script and catch in SI. That categoryName's type is Object and not stringgs.addInfoMessage(typeof categoryName)
is giving me Object.
I thing there is some problem there. But since it works with if-else I donno the issue.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-01-2022 08:07 AM
Hi,
Should work fine ideally in server side as well.
Did you debug by adding gs.info() statements?
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-02-2022 11:03 PM
I added a info message after catching the value (after this.getParameter) and I am getting the value there. But after that it doesn't enter in switch case. It skips the whole block of code.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-02-2022 11:11 PM
Hi,
Strange. You never know how ServiceNow script behaves. It's a classic example
Did you try to create the script again by removing the earlier one and adding the same again?
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-21-2023 07:50 AM
Not sure if you ever found a solution for this but I searched through existing SI's that had switch statements and found some were declaring the switch expression as String before the expression. So for your case:
switch (String(categoryName))
I was having this same issue and doing this worked for me.