Switch case statement in a script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-07-2017 05:42 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-09-2017 01:46 PM
I encountered a similar situation. I call the toString() method in my switch-case, however, I had to use " " rather ' ' to get the switch running.
Can you try this one?
var country = current.requested_for.country;
switch(country) {
case "GB" :
//your code here
default :
//your code here
}
Darshak
Edit: Thanks for the catch John. I didn't intend to put it there.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-09-2017 01:49 PM
There is nothing in your switch checking the value of temp. I think you may mean this:
- switch(bar.toString()) { // <-- added .toString()
- case "alpha":
- return "a";
- /* snip */
- default:
- return "unknown '" + bar + "'";
- }
I did tried this earlier today. No luck.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-09-2017 02:04 PM
I have looked at a script include I created and I've called this in a workflow. This worked for me.
this is the function in script include.
getRequestType : function(serviceType) {
var reqService = serviceType.toString();
var serv = '';
switch(reqService) {
case "alpha":
serv = 'a';
break;
case "beta":
serv = 'b';
break;
default:
serv = 'undefined';
}
return serv ;
},
And when I call this in workflow, it works fine.
I'm sorry I couldn't be of much help.
Darshak
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-09-2017 02:06 PM
Thank you for trying all that for me. I really do appreciate it. I'm still pretty confused by this whole thing. Ha.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-13-2019 07:13 AM
I could not get my switch to work until I changed my single quotes ' ' to double quotes " ". Interesting ServiceNow limitation, as I am in London, but thank you for pointing this out. Changing my quotes made my switch statement finally work.