Missed Team ’24? Catch up on announcements here.

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

Copy custom field "Customer" to Security Level field?

Eric Smith January 12, 2022

I am attempting to copy a custom field called "Customer" to the Security Level field in a workflow post-function using Jira Server 8.12. I have already made all the security levels to mimic the options in customer, but I cannot see a way to identify the security levels by name rather than ID. I was hoping to be able to call up a list of security levels in the script, match the security level by name, and use the associated ID to populate the request. Is this possible? Below is a rough idea of what I am trying to do:

 

import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.security.IssueSecurityLevelManager
import com.atlassian.jira.issue.security.IssueSecuritySchemeManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.security.IssueSecurityLevel
import com.atlassian.jira.issue.index.IssueIndexingService
import com.atlassian.jira.event.type.EventDispatchOption

def indexingService = ComponentAccessor.getComponent(IssueIndexingService.class)
def issueManager = ComponentAccessor.getIssueManager()
def user = ComponentAccessor.jiraAuthenticationContext.loggedInUser

def issue = event.issue as MutableIssue
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cf = customFieldManager.getCustomFieldObjects(issue).find {it.name == 'Customer'}
def cfvalue = issue.getCustomFieldValue(cf).toString()


issue.setSecurityLevelByName(cfValue)

 

1 answer

1 accepted

Suggest an answer

Log in or Sign up to answer
0 votes
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.
January 12, 2022

You'll need to fetch the IssueSecurityLevelManager to lookup your levels and filter for the correct one by name.

For example:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.security.IssueSecurityLevelManager
def customFieldManager = ComponentAccessor.customFieldManager
def issueManager = ComponentAccessor.issueManager
def securityLevelManager = ComponentAccessor.getComponent(IssueSecurityLevelManager)
def currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser

def issue = issueManager.getIssueObject('KEY-123')

def securityLevelName = 'My Level name'
def matchingSecurityLevel = securityLevelManager.getUsersSecurityLevels(issue.projectObject, currentUser).findByName(securityLevelName)

This will return the security level object that match the specified name for the specified issue.

You should be able to plug this into your script.

Eric Smith January 12, 2022

Sorry for my lack of understanding, I think im missing the part where the security level ID is realized. Plugging it in my script it still looks like im missing something. When I put the following into the script console, the output is simply "[com.atlassian.jira.issue.security.IssueSecurityLevelImpl@2723]". There are no security level IDs. And assuming it did output IDs, im still a bit fuzzy on how I would associate. Here is what I have in my console:

import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.security.IssueSecurityLevelManager
import com.atlassian.jira.issue.security.IssueSecuritySchemeManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.security.IssueSecurityLevel
import com.atlassian.jira.issue.index.IssueIndexingService
import com.atlassian.jira.event.type.EventDispatchOption

def indexingService = ComponentAccessor.getComponent(IssueIndexingService.class)
def issueManager = ComponentAccessor.getIssueManager()
def currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser

def issue = issueManager.getIssueObject('TEST-63')
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cf = customFieldManager.getCustomFieldObjects(issue).find {it.name == 'Customer'}
def cfValue = issue.getCustomFieldValue(cf).toString()
def securityLevelManager = ComponentAccessor.getComponent(IssueSecurityLevelManager)
def securityLevelName = 'Master Security Scheme'
//def matchingSecurityLevel = securityLevelManager.getUsersSecurityLevels(issue.projectObject, currentUser).findByName(securityLevelName)

//def matchingSecurityLevel = securityLevelManager.getUsersSecurityLevels(issue.projectObject, currentUser)

def matchingSecurityLevel = securityLevelManager.getAllSecurityLevelsForUser(currentUser)

return matchingSecurityLevel

 

Result:

"[com.atlassian.jira.issue.security.IssueSecurityLevelImpl@2723]"

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.
January 12, 2022

Are you familiar with using the JavaDocs?

This console script will return a IssueSecirtyLevel object. You can access that from the console with the CTRL-J shortcut

If you go to the link I just provided, you will see that such object has a getId() method.

Because we are using groovy, we can access the getters without the get keyword such that in your posFunction script, you can do the following:


issue.setSecurityLevelId(matchingSecurityLevel.id)
Eric Smith January 12, 2022

I am! Maybe not well-versed, but im at least dangerous :). CTRL-J and CTRL-SPACE have been helping me navigate since I found out about them a month or so ago. I put it all back in console and added the line "def securityLevelName = cfValue" so the security level name is coming from the custom field value, but it only ever gives me back 10017, which is the first alphabetical security level that my user is able to change. If I change the security level on the TEST-63 ticket to something other than the first security level alphabetically, the console output continues to result in 10017. Any ideas what im doing wrong? Script is below:

 

import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.security.IssueSecurityLevelManager
import com.atlassian.jira.issue.security.IssueSecuritySchemeManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.security.IssueSecurityLevel
import com.atlassian.jira.issue.index.IssueIndexingService
import com.atlassian.jira.event.type.EventDispatchOption

def indexingService = ComponentAccessor.getComponent(IssueIndexingService.class)
def issueManager = ComponentAccessor.getIssueManager()
def currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser

def issue = issueManager.getIssueObject('TEST-63')
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cf = customFieldManager.getCustomFieldObjects(issue).find {it.name == 'Customer'}
def cfValue = issue.getCustomFieldValue(cf).toString()
def securityLevelManager = ComponentAccessor.getComponent(IssueSecurityLevelManager)
def securityLevelName = cfValue
def matchingSecurityLevel = securityLevelManager.getUsersSecurityLevels(issue.projectObject, currentUser).findByName(securityLevelName)


return matchingSecurityLevel.id

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.
January 12, 2022

What type of custom field is your "Customer" customer field?

Is it a single select?

If so, then the "getCustomFieldValue" will return an "Option" object.
Your toString() method will coerce that object to a string using the optionId.

toString() is rarely the correct or more reliable way to extract data from an object.

Try this version of the script:

import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.security.IssueSecurityLevelManager
import com.atlassian.jira.issue.security.IssueSecuritySchemeManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.security.IssueSecurityLevel
import com.atlassian.jira.issue.index.IssueIndexingService
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.customfields.option.Option

def indexingService = ComponentAccessor.getComponent(IssueIndexingService.class)
def issueManager = ComponentAccessor.getIssueManager()
def currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser

def issue = issueManager.getIssueObject('TEST-63')
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cf = customFieldManager.getCustomFieldObjects(issue).find {it.name == 'Customer'}
def customerOption = issue.getCustomFieldValue(cf) as Option
def securityLevelManager = ComponentAccessor.getComponent(IssueSecurityLevelManager)
def matchingSecurityLevel = securityLevelManager.getUsersSecurityLevels(issue.projectObject, currentUser).findByName(customerOption.value)

return matchingSecurityLevel.id
Eric Smith January 12, 2022

Wow, I didnt know that! I have been using toString() for a lot of things I shouldnt have been, might explain some other unrelated issues I am having. I put the above script in console and changed the customer field and confirmed that it is in fact changing to different security levels based on the customer field. Thank you so much for your assistance and for letting me know I can use option instead of string!

TAGS
AUG Leaders

Atlassian Community Events