Add underscore _ to text string

makiz63
Kilo Contributor

I have the following script which is not working, I want to have an underscore after the 'Room" before the var_area is displayed

ie:  Room_ComptuterRoom301

if (loc1) {

desk +="Room" + "_" + 'var_area\n';

}

How do I join the symbol.

1 ACCEPTED SOLUTION

Okay, I see a few things wrong with this. First, loc1 and loc2 are booleans, not string, so the end result will be Room_true or Room_false, not Room_London or Room_NewYork.

 

But I'm still a bit confused over the checkbox thing. If there are two checkboxes, one for each city, there are four states in all: both selected, one selected, the other selected, and neither selected. It sounds like you might want to make that a drop-down choice list instead of checkboxes.

 

That having been said, I took a crack at rewriting your code to be a bit shorter, and hopefully accomplishing what you're trying to do. The caveat is that if both checkboxes are selected, it's going to stop at the first city ('London') without checking the other one. If neither checkbox is selected, it won't do anything. Here's the code:

 

function onSubmit() {
    var city = [ 'London', 'NewYork' ];
    var loc = 'Room' + (g_form.getValue('deskt') == 'perm' ? 'PM' : '') + '_';
    
    for (var i = 0; i < cities; i++) {
        if (g_form.getValue(city[i]) == 'true') {
            g_form.setValue('room', loc + city[i]);
            return;
        }
    }
    
    // If you've reached this point, no location was selected, so don't set
    // the room field value.
}

 

If the tertiary operator (?:) is confusing, all it does is evaluate the the value before the colon ('PM') if the expression to the left of the ? is true, or the value after the colon ('') if the expression to the left of the ? is false. It's just a syntactic shortcut for an if statement.

 

There are other ways to implement this, but this is flexible enough that if you add more checkboxes later, just add them to the array.

 

Hope this helps,

--Dennis R

View solution in original post

12 REPLIES 12

makiz63
Kilo Contributor

I tried putting the underscore like desk="Room_"+'var_area'; but it still doesnt work.  So strange it seems servicenow is reading the underscore as a special character and isnt processing it.

Dennis R
Tera Guru

If var_area is a variable, you need it outside the quotation marks, like:

 

desk += "Room" + "_" + var_area + "\n";

 

Other than that, the script looks fine to me also. In what context are you running this script? (That is, is it in a business rule, on a page using jelly, in a client script, etc.) Also, when you say, "on submit nothing is displayed of that variable," how are you checking that? Are you echoing it out to the log file or checking it in some other way?

 

I validated this in our own instance and it ran fine.

makiz63
Kilo Contributor

Thanks all for looking into this. So here is the full code I think its better. So I have a form with 2 checkboxes , if the checkbox is selected (true) it should display the output in a text field variable called 'Room' with the prefix text with an underscore


function onSubmit() {
var desktype = g_form.getValue('deskt');
var loc1 = (g_form.getValue('London') == 'true');
var loc2 = (g_form.getValue('NewYork') == 'true');
var selectedLocation = '';

if(loc1 && desktype =='temp'){
selectedLocation += "Room" + "_" + loc1 + "\n";// this should display output of Room_London

}
if (loc2 && desktype =='perm'){
selectedLocation += "RoomPM" + "_" + loc2 +"\n" // this should display output of RoomPM_NewYork
}
g_form.setValue('Room', selectedLocation); // 'room' is a text field where the output of the above is written onto when user submits the form
}

I might be missing something here, but can't you set selectedLocation as 100% string? 

 

if(loc1 && desktype =='temp'){
selectedLocation = "Room_London";

}
if (loc2 && desktype =='perm'){
selectedLocation = "RoomPM_NewYork";
}
g_form.setValue('Room', selectedLocation); // 'room' is a text field where the output of the above is written onto when user submits the form
}

 
Otherwise maybe try declaring the underscore as a variable first:
 
var uscore = '_' 
 

if(loc1 && desktype =='temp'){
selectedLocation += "Room" + uscore + loc1;  // this should display output of Room_London

}
if (loc2 && desktype =='perm'){
selectedLocation += "RoomPM" + uscore + loc2;  // this should display output of RoomPM_NewYork
}
g_form.setValue('Room', selectedLocation); // 'room' is a text field where the output of the above is written onto when user submits the form
}

 

I really think that \n is messing you up. Can you please try removing it and see if that works?