Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Based on the project role i want to hide some custom fields in view screen

Sreenivasulu September 24, 2024

Hi Team,

 

could you please help me 

Based on the project role i want to hide some custom fields in view screen using script runner.

 

These are the custom fields.

Assigned tester:  user picker

Test TL : user picker

Assigned Developer  user picker

DEV TL - user picker

2 answers

0 votes
Perumal M
Contributor
September 25, 2024

Hi @Sreenivasulu ,

"Could you please use the script below in Scriptrunner behavior? It will be helpful for you."

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.security.roles.ProjectRoleManager

// Get the Project Role Manager
def projectRoleManager = ComponentAccessor.getComponent(ProjectRoleManager)
def financeRole = projectRoleManager.getProjectRole("Finance") // Replace with your actual project role name

// Get the current user and project
def currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def project = issueContext.projectObject
def totalRevenueField = getFieldById(getFieldChanged())

// Debugging: Print out the current user and project
log.error("Current user: ${currentUser.username}, Project: ${project?.key}")

// Check if the user has the "Finance Team" role in this project
if (projectRoleManager.isUserInProjectRole(currentUser, financeRole, project)) {
    // Finance users: Show the field and make it editable
    totalRevenueField.setHidden(false)
    totalRevenueField.setReadOnly(false)

    log.error("User ${currentUser.username} is part of Finance Team. Showing Total Revenue field.")
} else {
    // Non-Finance users: Hide the field completely
    totalRevenueField.setHidden(true)
    totalRevenueField.setFormValue("Empty")
    log.error("User ${currentUser.username} is NOT part of Finance Team. Hiding Total Revenue field.")
}
Note: It will only support the edit and create screens. The behavior script won't directly hide the field in the view screen; you need to use the Scriptrunner fragment.
0 votes
Chris Rainey
Community Champion
September 24, 2024

Hi there,

Check out these YouTube videos on Behaviors in ScriptRunner and see if they assist you.

Video 1

Video 2

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
FREE
PERMISSIONS LEVEL
Product Admin
TAGS
AUG Leaders

Atlassian Community Events