How to get single select custom field values in Scriptrunner Behaviors?

Nishant V June 30, 2021

I'm currently trying to write a Scriptrunner Behavior script where I have to fetch the values of a single select custom field value and write conditions based upon the values I receive on "Create Issue" action.

 Below is the code that I have written:

 

/* Behaviour Script to update Description field value based on Request Types */

// Get Request Type field value
def requestTypeField = getFieldById("customfield_10424").getValue();

// Get Description field
def description = getFieldById("description");

// Set Description value if it is General Request
def descriptionGeneralRequest = """
General Request
""";

// Set Description value if it is New Table Request
def descriptionNewTableRequest = """
Test 
""";

// Set Description value if it is Ad hoc Burst Request
def descriptionAdHocBurstRequest = """
Test
""";

// Set Description value if it is Production Issue
def descriptionProductionIssue = """
Test
""";

// Condition to check if it is a create issue action
if (getActionName() == "Create") {

// Check if the Request Type is General Request
if(requestTypeField == "General Request") {

// Set Description value for General Request
description.setFormValue(descriptionGeneralRequest);

// Check if the Request Type is New Table Request
} else if (requestTypeField == "New Table Request") {

// Set Description value for New Table Request
description.setFormValue(descriptionNewTableRequest);

// Check if the Request Type is Ad hoc Burst Request
} else if (requestTypeField == "Ad hoc Burst Request") {

// Set Description value for Ad hoc Burst Request
description.setFormValue(descriptionAdHocBurstRequest);

// Check if the Request Type is Production Issue
} else if (requestTypeField == "Production Issue") {

// Set Description value for Production Issue
description.setFormValue(descriptionProductionIssue);
}
}


I'm unable to receive the custom field value and because of that the conditions are failing. Please let me know how to solve this. TIA.

 

2 answers

2 votes
Tessa Tuteleers
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 1, 2021

Hi @Nishant V , 

you can get the value of a single select like this: 

def singleSelectField = ComponentAccessor.customFieldManager.getCustomFieldObject(10402) // id of the CF
def singleSelectFieldValue = issue.getCustomFieldValue(singleSelectField)

If you then want to check the value, it's better to check it like this: 

def singleSelectField = ComponentAccessor.customFieldManager.getCustomFieldObject(10402) // id of the CF
def singleSelectFieldValue = issue.getCustomFieldValue(singleSelectField).toString()
return singleSelectFieldValue.equals("StringYouWantToCheck")

 - Tessa

Nishant V July 1, 2021

Variable "issue" is undeclared. 

Also, this behavior will be on "Create Issue" action. So I wonder how can we get Issue information even before creating it?

Nishant V July 1, 2021

@Tessa Tuteleers 

Do you mind helping me with the code? I'm stuck with this since yesterday. 

In the above script that I have mentioned what all changes I need to be doing?

Tessa Tuteleers
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 1, 2021

Hi @Nishant V , 

I have looked up 1 of my old scripts. 

This works as is in a behaviour: 

def singleSelectField = getFieldById("customfield_10402")
def singleSelectFieldValue = singleSelectField.getValue() as String

def descriptionField = getFieldById("description")

if(singleSelectFieldValue.equals("Condition 1")) {
descriptionField.setFormValue("Value 1");
} else if (singleSelectFieldValue.equals("Condition 2")) {
descriptionField.setFormValue("Value 2");
} else {
descriptionField.setFormValue(""); // fallback
}

You can rework this to your values. 

Good luck! 

- Tessa

Nishant V July 1, 2021

Hi @Tessa Tuteleers

I tried what you suggested but it is not working. It is going to the // fallback part directly and showing me only that value even when I change form values. If the values are being returned as String then I guess the conditions that we are writing must work. The only way that I'm seeing this now is the values are being returned in another format and not as String. 

Below is the code FYI:

def requestTypeField = getFieldById("customfield_10424");
def requestTypeFieldValue = requestTypeField.getValue() as String

def description = getFieldById("description");

def descriptionGeneralRequest = """
General Request
""";

def descriptionNewTableRequest = """
New Table Request
""";

if (getActionName() == "Create") {

// Check if the Request Type is General Request
if(requestTypeFieldValue.equals("General Request")) {

// Set Description value for General Request
description.setFormValue(descriptionGeneralRequest);

// Check if the Request Type is New Table Request
} else if (requestTypeField.equals("New Table Request")) {

// Set Description value for New Table Request
description.setFormValue(descriptionNewTableRequest);

} else {
// Set Description default value
description.setFormValue(descriptionGeneralRequest);
}
}

 Thanks

Tessa Tuteleers
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 1, 2021

@Nishant V , 

this "requestTypeField" is a normal singl select field, right? Not Customer request type? 

best you can do now is comment the entire if/else and add following: 

description.setFormValue(requestTypeFieldValue)

 That way you print what you are checking, and we get an idea of what's going on.

Nishant V July 1, 2021

Hi @Tessa Tuteleers

Yes! The requestTypeField that I'm using is not "Customer request type". It is a single select list custom field that we have created.  

Tessa Tuteleers
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 1, 2021

What gets printed out with the line of code in my earlier comment?

Nishant V July 1, 2021

Empty. There is nothing being set in description when I try the above line of code by commenting out everything else.

0 votes
Vikrant Yadav
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 2, 2021

Hi @Nishant V  As per my understanding you are trying to set Description based on Single Select List field value selection. Try to below code to set description using behaviour . Set server side script on Select List field.

 

def accuracy = getFieldByName("Accuracy") //select list field
def desc = getFieldById("description")

log.debug("dropdown value" + accuracy.getValue())
if (accuracy.getValue() == "Satisfied") {
desc.setFormValue("Test Case 1")
}else if (accuracy.getValue() == "Very Satisfied") {
desc.setFormValue("Test Case 2")
}else {
desc.setFormValue("Documentation subtask")
}

 

Suggest an answer

Log in or Sign up to answer