Switch statement

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-24-2009 10:24 AM
FWIW, I was looking for info to see if I could use a switch statement in a transform script rather than a bunch of if/else. I found no examples in the forums or wiki so I gave it a try. It worked! Here's my solution to take a single character from our HR database on a pay code (S, H, or E) and populate our own field with words.
switch (source.u_paycode.toString()) {
case 'S': target.u_paycode = "Salaried Exempt"; break;
case 'E': target.u_paycode = "Salaried Non-Exempt"; break;
case 'H': target.u_paycode = "Hourly"; break;
default: target.u_paycode = "Hourly";
}
Switch statements are an elegant solution. These work just as expected in JavaScript.
- 11,045 Views

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-27-2017 05:59 AM
Hi Stacey,
Keep in mind that this was written 8 years ago. I've learned a few things since that time. In most cases when dealing with field or variable values, I recommend using "getValue()" instead. The key is to remember that fields are objects. Just using toString() is forcing a data type change. Yeah, it USUALLY works, but it just feels wrong from a programmer's perspective when getValue() is available to extract the exact value for you. Using getters and setters to manipulate the object and it's properties is the correct way to do it - of course not all objects provide a getValue() method, but in the case of GlideRecord and GlideElement, it's there so fields should really use getValue() for clarity and flexibility.
Example:
var payCode = source.getValue('u_paycode');
answer=""
switch (payCode) {
case 'S': answer = "Salaried Exempt"; break;
...
}
yes, I said flexibility because you can always use a string variable as an argument to getValue(). For example, provide a list of field names, ['state', 'short_description', 'priority'] and pass those to getValue() to get the values of just those fields. You cannot do that with toString().

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-25-2017 09:53 PM
Chuck, I'm trying to transform a field that has multiple entries and I want to leverage the out of the box field "Used for" values. Given the below script, can you help me understand why it's not working?
answer = (function transformEntry(source) {
if(!JSUtil.nil(source.u_environmentname)){
switch (source.u_environmentname.toString().toLowerCase()) {
case 'demo & pursuit':
case 'sandbox':
case 'unmonitored (sandbox)': answer = "Demonstration"; break;
case 'development':
case 'new server build':
case 'unmonitored (pre-build)': answer = "Development"; break;
case 'business continuity':
case 'staging / bcp': answer = "Disaster Recovery"; break;
case 'quality assurance':
case 'qa': answer = "QA"; break;
case 'qa/staging':
case 'staging':
case 'unmonitored (pre-production)': answer = "Staging"; break;
case 'lab': answer = "Test"; break;
case 'training': answer = "Training"; break;
default: answer = "Production";
}
}
return ""; // return the value to be put into the target field
})(source);

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-26-2017 05:48 AM
Hi Philip,
I don't see anything offhand that could be an issue. When I get in to situations like this, I like to break out the variable I'm switching on and display it in the logs. Example:
var env = source.u_environmentname.toString().toLowerCase();
gs.info('Environment=' + env);
switch (env) {
// cases
// default
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-26-2017 03:12 PM
Thanks for the gs.info tip as that has proved that the switch case is working as expected, but for some reason the value being returned is not updating the column "Used by". It's an out of the box column so it doesn't make sense that it's not updating. Still digging.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-28-2017 07:57 PM
Chuck, thought you might like to know I fixed my own bug. If you look at my code the switch case was returning the answer, but when it exist my last statment of return wall nullifying my answer. So all I had to do was change it from:
return "";
to
return answer;
all fixed and it works great!