Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

Creating an if else statement in Record Producer where it won't select the first option, automatically

Jeff Crais
Kilo Contributor

Hi,

I'm having an issue where I am trying to create an if-else statement in Record Producers against a multiple-choice, to be seen in an Incident ticket's short description field. Even though I select option two, it displays the variable for option 1. If I just use If statements, it's the reverse, it selectin option 3.

What am I doing wrong?

var shortDescriptionValue1 = producer.short_description.getDisplayValue() + ' issue' + ' for ' + producer.caller_id.getDisplayValue() + ', Vehicle #: ' + producer.vehicle_number;

//example: "Mobile Radio issue for John Doe, Vehicle #: 1234"

var shortDescriptionValue2 = producer.short_description.getDisplayValue() + ' issue' + ' for ' + producer.caller_id.getDisplayValue() + ', Property or Serial Number #:' + producer.property_number.getDisplayValue();

//example: "Handheld/Portable Radio issue for John Doe,  Property or Serial Number #: 1234"

var shortDescriptionValue3 = producer.short_description.getDisplayValue() + ' issue' + ' for ' + producer.caller_id.getDisplayValue();

//example: "Dispatch Consoles issue for John Doe"

if(producer.short_description == 'mobile_radio');
{

current.short_description=shortDescriptionValue1;
} elseif(producer.short_description == 'handheld_portable radio');
{
current.short_description=shortDescriptionValue2;
} elseif(producer.short_description == 'dispatch_consoles');
{

current.short_description=shortDescriptionValue3;
}

6 REPLIES 6

Matthew Glenn
Kilo Sage

Not sure if this will fix your issue, but there are a few small problems with the code you pasted. 

To start, "Else If" is two words, not one. And after your "If" and "Else If" conditions, remove the semi-colons. 

There may also be a consistency issue with your condition in your first "Else If" statement. Seeing the underscores in your choice list values and then that seemingly one-off space - it stands out. Might be worth checking out.

Code should look like this (note I didn't change the choice list value in your 2nd Else If condition):

var shortDescriptionValue1 = producer.short_description.getDisplayValue() + ' issue' + ' for ' + producer.caller_id.getDisplayValue() + ', Vehicle #: ' + producer.vehicle_number;
//example: "Mobile Radio issue for John Doe, Vehicle #: 1234"

var shortDescriptionValue2 = producer.short_description.getDisplayValue() + ' issue' + ' for ' + producer.caller_id.getDisplayValue() + ', Property or Serial Number #:' + producer.property_number.getDisplayValue();
//example: "Handheld/Portable Radio issue for John Doe,  Property or Serial Number #: 1234"

var shortDescriptionValue3 = producer.short_description.getDisplayValue() + ' issue' + ' for ' + producer.caller_id.getDisplayValue();
//example: "Dispatch Consoles issue for John Doe"

if (producer.short_description == 'mobile_radio') {
    current.short_description = shortDescriptionValue1;
} else if (producer.short_description == 'handheld_portable radio') {
    current.short_description = shortDescriptionValue2;
} else if (producer.short_description == 'dispatch_consoles') {
    current.short_description = shortDescriptionValue3;
}

 

Plug that in and try again. Like I said, not sure if it's the fix, but your code won't work until at least the "Else If" correction is made

 

Edit: You may want to throw in an "Else" statement after your last "Else If". It may not be required, but it's a good practice to get into in the event a value comes across that does not match your existing conditions

I'm getting an error message in regards to having a space between else if:

find_real_file.png

Any ideas?

Remove the semi-colons

 

find_real_file.png

Edit: Not pictured in my image above, but you also need to remove the semi-colon in line 13 (your If statement)

Thanks. I overlooked that. Still learning ServiceNow and JS.