The Zurich release has arrived! Interested in new features and functionalities? Click here for more

switch case in servicenow script

Vicky Kumar Sh1
Tera Contributor

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;
//         }
9 REPLIES 9

MrMuhammad
Giga Sage

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

Regards,
Muhammad

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

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)
Regards,
Muhammad

Sandeep Rajput
Tera Patron
Tera Patron

Can you check if you are getting correct value within categoryName, I simply copied and pasted your script (with switch case) and it ran fine on my PDI.

find_real_file.png