Record producer script, reuse variable based on field value

AlexGuld
Mega Guru

Hi

I need some help with my script.

I have 3 fields that helps me define if the record producer should create an incident or a request.

 

The hole script is contained in an if condition, that look to the value of the 3 fields. It started as 1 field and that's easy to make an if condition but now I need to look at 3 fields to deduce if it should be an incident or request.

I have made it so the values of the 3 fields start with either req or inc, only one field is used at a time based on a different field.

So, the user chooses X in field A and that shows field 1 that has values starting with req and inc.

if user then chooses Y in field A then field 2 is shown and that also has values starting with req and inc.

and if the user chooses Z ind field A the field 3 is shown and that also has values starting with req and inc.

 

What is the best way to look at the value of either of the 3 fields to tell if it should be an incident or request.

my if condition to see if the value starts with req works.?

 

 

if (producer.field1.toString().startsWith('req'))

 

(field1 is not the real value just to make it easier to explain)

 

I have tried using if else looking at the 3 fields and only placing the value in a variable if it's not empty, but that did nok work. The idea was to use the same variable but with different value based on the users indput as only on of field1, field2, field3 is shown at a time and therefor the 2 fields not shown are not selected and empty. 

 

 

    if (producer.field1 != '') {
        var case_type = producer.field1.toString();
    } else if (producer.field2 != '') {
        var case_type = producer.field2.toString();
    } else if (producer.field3 != '') {
        var case_type = producer.field3.toString();
    }

 

 

I'm unsure if that work's as the variable case_type gets declared 3 times, but I can't think of another way to use the same variable.

1 ACCEPTED SOLUTION

Sid_Takali
Kilo Patron
Kilo Patron

Hi @AlexGuld Try below code

var case_type = '';
if (producer.field1 && producer.field1.trim() !== '') {
    case_type = producer.field1.toString();
} else if (producer.field2 && producer.field2.trim() !== '') {
    case_type = producer.field2.toString();
} else if (producer.field3 && producer.field3.trim() !== '') {
    case_type = producer.field3.toString();
}

if (case_type.startsWith('req')) {
    gs.log('Create a request');
} else if (case_type.startsWith('inc')) {
    gs.log('Create an incident');
} else {
    gs.log('Invalid input');
}

 

Regards,

Sid

View solution in original post

1 REPLY 1

Sid_Takali
Kilo Patron
Kilo Patron

Hi @AlexGuld Try below code

var case_type = '';
if (producer.field1 && producer.field1.trim() !== '') {
    case_type = producer.field1.toString();
} else if (producer.field2 && producer.field2.trim() !== '') {
    case_type = producer.field2.toString();
} else if (producer.field3 && producer.field3.trim() !== '') {
    case_type = producer.field3.toString();
}

if (case_type.startsWith('req')) {
    gs.log('Create a request');
} else if (case_type.startsWith('inc')) {
    gs.log('Create an incident');
} else {
    gs.log('Invalid input');
}

 

Regards,

Sid