Switch case statement in a script

jasleen3
Kilo Contributor

Hi,

My script is running fine if I am using "if statement", but if i use switch it's not working at all.

find_real_file.png

19 REPLIES 19

Rama Chandra D
Kilo Guru

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.


There is nothing in your switch checking the value of temp. I think you may mean this:



  1. switch(bar.toString()) { // <-- added .toString()  
  2.         case "alpha":  
  3.                   return "a";  
  4.         /* snip */  
  5.         default:  
  6.                   return "unknown '" + bar + "'";  
  7. }


I did tried this earlier today. No luck.


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


Thank you for trying all that for me. I really do appreciate it. I'm still pretty confused by this whole thing. Ha.


Community Alums
Not applicable

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.