ScriptRunner how to set issue security level based on custom field

Yuriy Tsymbaliyk November 30, 2018

Hi Atlassian Community, im looking for solution to set Issue security Level based on custom field value.

 

I find the best way to do this by using ScriptRunner workflow function:

  • Set issue security level depending on provided conditionHelp
    Sets issue security if the provided condition evaluates to true

Description:

We have custom filed "Teams".

We have values "Team first"; "Team Second"

We have issue security level, and one of this level which called "Administration Task SL"

Also i find id of this custom field value id of one of parameter "Team first = 11700"

 

But im new in this scripts and always get an error:

 

Screenshot_3.png

All screen how i try to do this

1) use transition on which i need to change issue security level

2) try to add postfunction from ScriptRunner workflow function (Set Issue security level depending on provided condition)

3) SetName of security level

4) Got issue's in code

Screenshot_2.pngMaybe im doing something wrong or not define some values.

Here are my code:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.security.IssueSecurityLevelManager
import com.atlassian.jira.issue.security.IssueSecuritySchemeManager

def project = issue.getProjectObject()
def projectComponentManager = ComponentAccessor.getProjectComponentManager()
def issueSecuritySchemeManager = ComponentAccessor.getComponent(IssueSecuritySchemeManager)
def issueSecurityLevelManager = ComponentAccessor.getComponent(IssueSecurityLevelManager)

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def issueManager = ComponentAccessor.issueManager

def cfValue =customFieldManager.getCustomFieldObjectByName("Teams")
if (cfValue == 'Team first') {
issue.setSecurityLevelId(11700)
}

 

Maybe somebody meet this before? or can help me how to correct define condition?

 

 

Or somebody know how to make, becose we need to use one of two issue security level based on custom field. Maybe i need something different?

if (cfValue == 'Team one') {
issue.setSecurityLevelId(null)
} else if (cfValue == 'Team second') {
issue.setSecurityLevelId(secLevel.id)
} else {
//something else
}

 

3 answers

1 accepted

0 votes
Answer accepted
Deleted user March 22, 2022

Hi to all, I am trying to do a similar thing, I have this code that works, in the sense that it writes in the custom field security level the value taken from the field Group Assignee, group picker. But every time I complete the transition between two states of the workflow, I always find the previous value of the group picker and not the current one, as if I was forgetting to write something to force the update of the issue. Since I'm still a newbie in Script Runner, I'm asking if you can help me to understand where I'm going wrong.

Schermata 2022-03-22 alle 08.41.38.pngSchermata 2022-03-22 alle 08.33.42.png

 

Schermata 2022-03-22 alle 08.51.05.png

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.security.IssueSecurityLevelManager
import com.atlassian.jira.issue.security.IssueSecuritySchemeManager

import com.atlassian.crowd.embedded.impl.ImmutableGroup

def issueSecuritySchemeManager = ComponentAccessor.getComponent(IssueSecuritySchemeManager)
def issueSecurityLevelManager = ComponentAccessor.getComponent(IssueSecurityLevelManager)
def customFieldManager = ComponentAccessor.customFieldManager

// Change it to your Group Picker field name
def groupPicker = customFieldManager.getCustomFieldObjectsByName("Gruppo Assegnatario").first()
def group = (issue.getCustomFieldValue(groupPicker) as ArrayList<ImmutableGroup>).first()

def project = issue.getProjectObject()
def issueSecurityScheme = issueSecuritySchemeManager.getSchemeFor(project)
def securityLevelID = issueSecurityLevelManager.getIssueSecurityLevels(issueSecurityScheme.id).find { it ->
// since the group name is equal to the security level name
it.name == group.name
}?.id

issue.setSecurityLevelId(securityLevelID)

1 vote
Tom Lister
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 30, 2018

Hi @Yuriy Tsymbaliyk

You don't need to explicitly set the security level using 'Set Issue security level depending on provided condition'

Just set the condition and the security level

Add multiple functions with mutually exclusive conditions

Screenshot 2018-11-30 at 14.29.23.png

Yuriy Tsymbaliyk November 30, 2018

Hi Tom!

I try this example, few time before, i got next error:

 

cfValues['Art Team & Position']?.value == '3D Photoreal Characters Team'

 

Screenshot_8.png

Tom Lister
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 30, 2018

Hi

try

cfValues['Group Field']?.name == 'Team First'

Yuriy Tsymbaliyk November 30, 2018

The same issue, not help.

Tom Lister
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 30, 2018

Hi

What is the type of custom field you are checking? It will need to match the field object properties

.name works on my server with a Group Single select.

Tom Lister
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 30, 2018

Just tested again. On my server, for a simple select list .value works.

cfValues['Select test']?.value == 'One'

The full error message tells you what object is being returned and you can check available getters in the API docs

0 votes
Elifcan Cakmak
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 30, 2018

Hello,

It seems like the error is "Cannot find matching method" because you are using int for ID. But setSecurityLevelId uses long for id. See below link:

https://docs.atlassian.com/software/jira/docs/api/7.1.1/com/atlassian/jira/issue/MutableIssue.html#setSecurityLevelId-java.lang.Long-

If you change it to 

 issue.setSecurityLevelId(11700L)

that problem should be solved.

Regards,

Elifcan

Yuriy Tsymbaliyk November 30, 2018

I try another more simple way it, no error but issue securite doesnt update

import com.atlassian.jira.component.ComponentAccessor

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def CC = customFieldManager.getCustomFieldObject("customfield_15001")
def CC_value = issue.getCustomFieldValue(CC).toString()

if (CC_value == '3D Team')
{
issue.setSecurityLevelId(11700L)
}
else if (CC_value == 'ArtTeam')
{
issue.setSecurityLevelId(11702L)
}

 

Also maybe steps not set up correct?

Screenshot_9.png

Tom Lister
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 30, 2018

see 

https://scriptrunner.adaptavist.com/latest/jira/builtin-scripts.html#_set_issue_security

 

"for the create transition, above the "Creates the issue originally" function"

Yuriy Tsymbaliyk November 30, 2018

I change steps, but i look to configuration of field

this field named: Select List (cascading)

 

I think i have trouble with this(

 

Screenshot_13.png

I think i need use id?   not   'some text value' ???

Tom Lister
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 30, 2018

Hi

before i go digging, did you intend to use a cascading select and are you looking to match the first level value against your team name?

Tom Lister
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 30, 2018

this will log the cascade fields values.  its the weekend now :-) good luck

 

import com.atlassian.jira.issue.customfields.view.CustomFieldParams
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.customfields.option.Option
import com.atlassian.jira.issue.customfields.impl.CascadingSelectCFType

import org.apache.log4j.Category

log.setLevel(org.apache.log4j.Level.DEBUG)

CustomField cf = customFieldManager.getCustomFieldObjectByName("Cascade test")

Object cfVal = issue.getCustomFieldValue(cf)
log.debug("" +cfVal)

HashMap<String, Option> hashMapEntries = (HashMap<String, Option>) cfVal
log.debug("" + hashMapEntries)

if (hashMapEntries != null) {
Option parent = hashMapEntries.get(CascadingSelectCFType.PARENT_KEY)
Option child = hashMapEntries.get(CascadingSelectCFType.CHILD_KEY)
def first = parent.toString()
def second = child.toString()
log.debug("Cascading values selected: $first - $second")
}

Suggest an answer

Log in or Sign up to answer