Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How to check if a component matches a required value in Behaviors for Service Desk?

Pavel October 6, 2023

Hello,

I struggle with "if" statement and components in Behaviors for Service Desk.

My goal is to make the Events field mandatory if there's a required value in Components and Company fields. It must happen on the customer portal during a request creation, not in a created issue.

The script I work on:

import com.onresolve.jira.groovy.user.FieldBehaviours

import com.atlassian.jira.issue.Issue
import com.atlassian.jira.component.ComponentAccessor
import groovy.transform.BaseScript

@BaseScript FieldBehaviours fieldBehaviours
def issue = getIssueContext()
def company = getFieldByName('Company')
def components = getFieldByName('Components')
def events = getFieldByName('Events')
String companyval = company.getValue()
String componentsval = components.getValue()
if (companyval == "Company name" && componentsval == ('value'))
{
    events.setRequired(true)
}
I understand that the way I work with components is wrong, what should I do in this case? Actually, if there's only companyval in the "if" statement, then it works ok but it breaks with components.

1 answer

0 votes
Tuncay Senturk _Snapbytes_
Community Champion
October 9, 2023

Hi @Pavel 

Component field returns multiple values (array/list), that's why you can't match the value of the field with a single value.

Try this one below

if (company.getValue()?.find { it.name == "Company name" }) {
events.setRequired(true)
}
Pavel October 9, 2023

Thank you so much! However, the code doesn't work, I tried to change it to fit my code, but no luck. Is there any other way how to check if array/list contains a required value? I know that I also can use company.contains, but it doesn't work too.

Tuncay Senturk _Snapbytes_
Community Champion
October 9, 2023

Both the contains() and the find() methods should work.

Could you please add some logs to understand if the code has been through the if clauses, etc?

Pavel October 11, 2023

Hi, is there a good way to add logs you need to the code? The are many ways, which one is preffered?

Pavel October 18, 2023

Any update pls

Tuncay Senturk _Snapbytes_
Community Champion
October 18, 2023

Actually, it is up to your preference. You can use any logging approach to see how your code works. For instance, you can use log.error('log details') method.

Like Pavel likes this
Pavel October 18, 2023

It has been solved:

if (company.getValue().toString().contains("One"))
Tuncay Senturk _Snapbytes_
Community Champion
October 18, 2023

Are you sure? I don't think toString() could be apprepriate there. 

Pavel October 18, 2023

However, it works :) I tried many ways

Suggest an answer

Log in or Sign up to answer