Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

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

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
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Oct 09, 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)
}

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
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Oct 09, 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?

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

Any update pls

Tuncay Senturk
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Oct 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

It has been solved:

if (company.getValue().toString().contains("One"))
Tuncay Senturk
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Oct 18, 2023

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

However, it works :) I tried many ways

Suggest an answer

Log in or Sign up to answer