...
11. Script to hide the custom fields on create screen if the user is not a project admin (behaviors - Server)
Code Block | ||
---|---|---|
| ||
/* Script to hide the "Notes" field if the current logged in user is not in "Project Owner" role and show it if the user is in "Project Owner" role. User can be in multiple roles but if he/she is not part of "Project Owner" role field should not be shown. */ import com.atlassian.jira.user.ApplicationUser import com.atlassian.jira.component.ComponentAccessor import com.atlassian.jira.security.roles.ProjectRoleManager import com.atlassian.jira.security.roles.ProjectRole import com.atlassian.jira.project.ProjectManager import com.atlassian.jira.user.util.UserManager //define the project role final String PROJECT_OWNER = "Project Owner"; //define the project name final String PROJECT_NAME = "TnT Jira Support Services"; //settingset flags to show and hide field final boolean NOTES_VISIBLE = true; final boolean NOTES_NOT_VISIBLE = false; //customdefine the field nameto be shown/hidden final String NOTES = "Notes"; //to get the current logged in user def loggedInUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser(); //get projectManagerProjectManager class def projectManagerClass = ComponentAccessor.projectManager; def projectRoleManager = ComponentAccessor.getComponent(ProjectRoleManager); //get the project role object of logged in user ProjectRole newRole = projectRoleManager.getProjectRole(PROJECT_OWNER); //get the project object of the project against which logged in user roles are checked def project = projectManagerClass.getProjectObjByName(PROJECT_NAME); //get the custom field def notesField = getFieldByName(NOTES); //check if the logged in user belongshas toa aproject role in a project, returnsreturn boolean def projectRoles = projectRoleManager.isUserInProjectRole(loggedInUser, newRole, project); //conditionscondition to hideset andthe show custom field basedhidden onor projectshown role if(projectRoles.equals(NOTES_VISIBLE)) { notesField.setHidden(NOTES_NOT_VISIBLE); } else { notesField.setHidden(NOTES_VISIBLE); } |
Learning:
Knowledge on how script-runner listener works for both cloud and server
Got to understand different scenarios for Jira based on which scripts were written
Basic knowledge on groovy
While setting the value for multi-select system fields always set the values based on “id’s of values” and not “values itself“ in behaviors
While setting the value for single select system fields we can directly give “values” instead of “id’s of values”
script-runner behaviors
While setting the value for single select custom field we have to give “id’s of values” only (can also be directly configured in field setting as these are custom fields)
...