Forums

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

How to hide a custom field in view screen through script runner.

N Sreenivasulu Reddy September 26, 2024

Hi,

How to hide a custom field based on user role or group in view screen only through script runner. Any possible solution will be helpful.

Thank you,

2 answers

0 votes
Ashok Shembde
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.
September 26, 2024

Hi @N Sreenivasulu Reddy ,

You can try this Script.

import com.atlassian.jira.component.ComponentAccessor
def user = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def userGroups = ['group1', 'group2'] 
def userRoles = ['role1', 'role2'] 
def customField = getFieldByName('CustomFieldName')
def groupManager = ComponentAccessor.groupManager
def projectRoleManager = ComponentAccessor.getComponent(ProjectRoleManager)
def currentIssue = underlyingIssue
def project = currentIssue?.projectObject
def isUserInRole = projectRoleManager.isUserInProjectRole(user, projectRoleManager.getProjectRole('role1'), project)
if (groupManager.isUserInGroup(user, 'group1') || isUserInRole) {
customField.setHidden(true)
} else {
customField.setHidden(false)
}

N Sreenivasulu Reddy September 26, 2024

Hi @Ashok Shembde ,

 

Hope you are doing well.

As we are achieving based on the user role the Originator field should not be visible in view screen as we tried but i am unable to achieve. pasted the script below.

 

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

// Get the currently logged-in user
def user = ComponentAccessor.jiraAuthenticationContext.loggedInUser

// Define the groups and roles to check
def userGroups = ['banned']
def userRoles = ['OPUS-BA-TEAM']

// Get the custom field by name
def customField = getFieldByName('Originator')

// Access the group and project role managers
def groupManager = ComponentAccessor.groupManager
def projectRoleManager = ComponentAccessor.getComponent(ProjectRoleManager)

// Get the current issue
def currentIssue = underlyingIssue
def project = currentIssue?.projectObject

// Check if the user is in the specified project role
def isUserInRole = projectRoleManager.isUserInProjectRole(user, projectRoleManager.getProjectRole('OPUS-BA-TEAM'), project)

// Check if the user is in the banned group or the specific project role
if (groupManager.isUserInGroup(user, 'banned') || isUserInRole) {
    // Hide the custom field if conditions are met
    customField.setHidden(true)
} else {
    // Show the custom field if conditions are not met
    customField.setHidden(false)
}
Please verify and i am unable to save the script.
Thanks
Sreenivasulu Reddy N
0 votes
said kouzibry
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.
September 26, 2024

This is something that can be achieved through Behaviors, although it is a seperate App on Cloud, it can be added to script runner (at no additional cost). I haven't tried this myself but the documentation shows you can show/hide fields on the issue view, take a look at the following links

 

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events