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

Defining a Simple Script Condition in the Insight field

Didem Seda Taş March 15, 2022

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

Suggest an answer

Log in or Sign up to answer
1 vote
Answer accepted
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 15, 2022

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
Didem Seda Taş March 16, 2022

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 :)

Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 16, 2022

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

Didem Seda Taş March 16, 2022

Hi Peter,

You are great! Your answer fully satisfied my request.

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

Didem Seda Taş March 23, 2022

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 :)

Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 24, 2022

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
Didem Seda Taş March 24, 2022

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

Didem Seda Taş March 28, 2022

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
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 28, 2022

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.

Didem Seda Taş March 28, 2022

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

Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 29, 2022

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
}
}
0 votes
Didem Seda Taş March 15, 2022

s

TAGS
AUG Leaders

Atlassian Community Events