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
‎07-29-2022 11:19 AM
Hi,
Your script looks good. Can you please try logs inside the cases? gs.addInfoMessage won't work in script include so please try gs.info("case Inquiry: " + categoryName);
Sample
gs.info("Category Name: " + categoryName);
gs.info("Category type: " + typeof categoryName);
switch (categoryName.toString()) {
case "inquiry":
assignDetail.groupSysID = '679434f053231300e321ddeeff7b12d8';
gs.info("Case Inquiry: " + categoryName);
break;
Please share the logs that you get so that I can further help you troubleshoot.
Please mark this helpful/correct, if applicable.
Regards,
Muhammad
Muhammad
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-02-2022 11:01 PM
since I'm using this SI in client side onChange of category. I checked with gs.addInfoMessage. That works.
So, I tested your code and Noticed two things:
1. typeof categoryName is giving me object. Shouldn't it give me String?
But even with that, It works with if-else, so that shouldn't be the problem.
2. I added a info message after catching the value (after this.getParameter) and I am getting the value there.
It just that after that line it doesn't enter in switch case

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-04-2022 10:15 PM
If its returning Object then it won't work until we explictly convert this to string as case is comparing it with string value so object is not equal to string.
Have you tried with toString() method? i.e.
switch (categoryName.toString())
If that doesn't work then try with JSON.stringify()
switch (JSON.stringify(categoryName)
Muhammad

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-01-2022 07:30 AM