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

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,467,416
Community Members
 
Community Events
177
Community Groups

Defining a Simple Script Condition in the Insight field

Hi There,

I have an object named "Business Service" and I have an attribute called 'Billing Service' in this object.

I have an insight field called "Affected Business Service" on the Create screen. If the user selects the 'Billing Service' option in this field while creating an issue, I want to define a post function so that my issue is assigned to the user in the project role I selected.

How can I define the situation I mentioned above as a simple script condition in the insgiht field?

If 'Billing Service' is selected in the "Affected Business Service" field on the create screen as in the attached, I want to write a simple script condition that will ensure that the issue is assigned to the user in the role I want.

image (7).png

Thanks for help.

Have a nice day,

Seda

2 answers

1 accepted

1 vote
Answer accepted

In a simple scripted condition, the cfValues['Affected Business Service'] will return an array of ObjectBean. This is true whether or not the custom field allows multiple.

But not when the fied is empty.

This is how I generally get those values so that I consistently have the right kind of data:

def values = (cfValues['Affected Business Service'] ?: []) as ArrayList<ObjectBean>

But this requires some additional imports and declarations and a quick null check.

Altogether, this will do what you want:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.security.roles.ProjectRoleManager
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import com.riadalabs.jira.plugins.insight.services.model.ObjectBean

@WithPlugin('com.riadalabs.jira.plugins.insight') insightPlugin
def values = (cfValues['Affected Business Service'] ?: []) as ArrayList<ObjectBean>

if (values && values[0].label == 'Billing Service') {
def currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def projectRoleManager = ComponentAccessor.getComponent(ProjectRoleManager)
def role = projectRoleManager.getProjectRole('Your Role')
return projectRoleManager.isUserInProjectRole(currentUser, role, issue.projectObject)
}
true

Hi Peter,

Thank you very much for your answer.

I gave my request to assign a role as an example. Actually I want a much simpler script.
If the user selects the Billing Service option in the Affected Business Service field, I just want to define it as this switch.

How can I write for it?

When I write SImple script conditiondan cfValues['Affected Business Service'] = Billing Service, the code works incorrectly. What should I add here?

 

Have a good day :)

The simplest approach would be to use the following:

cfValues['Affected Business Service'].any{it.label == 'Billing Service'}

If you just attempt to compare the cfValues as if it was a plain string, it would look like 

[Billing Service (KEY-123] where key-123 is the key for your billing service object.

That is because the data in insight custom fields are always "Collection of ObjectBean"

You could assume a single value:

cfValues['Affected Business Service'][0].label == 'Billing Service'

But this will break if you have not selected anything

Hi Peter,

You are great! Your answer fully satisfied my request.

Thanks a lot for your help!
Have a nice day :)

Hi Again Peter,

 

When I select the 'Billing Service' option in the "Affected Business Service" field of the insight type and the "Accounting" option in the "Related Section" field of the select list(single choice) type, I need a script that I will use in the Script runner plugin that will ensure that the record is assigned to the user in the "PMO" role.

 

I used this code in Assign to last role member post funtciton, but it didn't work.

cfValues['Affected Business Service'].any{it.label == 'Billing Service'} && 
cfValues["Related Section"] == "Accounting"

Is the code I wrote wrong, or is the post function I'm using wrong? What could be the reason for my error?

Can you help with this?

 

Have a nice day :)

Select List custom field return an "Option" object. 

So in the script, you need to compare that option's "value" attribute against an expected string.

Try

cfValues['Affected Business Service'].any{it.label == 'Billing Service'} &&
cfValues["Related Section"].value == "Accounting"

When the line becomes too long if you have to add additional criteria, it helps to break it down:

def isBillingService = cfValues['Affected Business Service'].any{it.label == 'Billing Service'}
def isAccounting = cfValues["Related Section"].value == "Accounting"

isBillingService && isAccounting

Thanks again Peter, you have been very helpful again :)

Hi again and again Peter,

I've come a long way with your answers. I have one more question. When the 'Billing Service' option is selected in the 'Affected Business Service' field, I want the record to be assigned to the reporter. How do I need to make changes to the script below that you wrote in your first reply?

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.security.roles.ProjectRoleManager
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import com.riadalabs.jira.plugins.insight.services.model.ObjectBean

@WithPlugin('com.riadalabs.jira.plugins.insight') insightPlugin
def values = (cfValues['Affected Business Service'] ?: []) as ArrayList<ObjectBean>

if (values && values[0].label == 'Billing Service') {
def currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def projectRoleManager = ComponentAccessor.getComponent(ProjectRoleManager)
def role = projectRoleManager.getProjectRole('Your Role')
return projectRoleManager.isUserInProjectRole(currentUser, role, issue.projectObject)
}
true

Simple Script Condition shouldn't change anything on the issue.

They should only be used to determine if the transition button should be displayed when viewing the issue.

If you are trying to change the reporter in a Post Function, then that's a whole other scenario.

There is no built-in function that sets the assignee as the reporter. So you have to use a "custom script post-function". And the cfValues shortcut is not available in that context. So the script has to be a bit more complex.

Is it possible to define a post function, is it too complicated?

It's not that complicated, it was just a switch from your initial description.

Here is a short Custom Post Function script that should achieve what you described:

import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import com.riadalabs.jira.plugins.insight.services.model.ObjectBean
@WithPlugin('com.riadalabs.jira.plugins.insight') insightPlugin
def customFieldManager = ComponentAccessor.customFieldManager

def affectedServiceCf = customFieldManager.getCustomFieldObjectsByName('Affected Business Service')[0]
def affectedServiceVal = issue.getCustomFieldValue(affectedServiceCf) as List<ObjectBean>

//make sure a value is selected
if
(affectedServiceVal){
//look at the label for the first item selected (even when max cardinality =1, we have a list of 1 item)
if(affectedServiceVal[0].label == 'Billing Service' ){
issue.assigneee = issue.reporter
}
}

Suggest an answer

Log in or Sign up to answer
TAGS

Atlassian Community Events