Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagegroovy
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

def loggedInUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def isAdmin = false
def projectManagerClass = ComponentAccessor.projectManager
def projects = projectManagerClass.getProjects()
def import com.atlassian.jira.project.ProjectManager
import com.atlassian.jira.user.util.UserManager

//the project role 
final String PROJECT_OWNER = "Project Owner";
//the project
final String PROJECT_NAME = "TnT Jira Support Services";

//setting flags
final boolean NOTES_VISIBLE = true;
final boolean NOTES_NOT_VISIBLE = false;

//custom field name
final String NOTES = "Notes";

//to get the current logged in user
def loggedInUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();
//get projectManager class
def projectManagerClass = ComponentAccessor.projectManager;
def projectRoleManager = ComponentAccessor.getComponent(ProjectRoleManager);
//get project role object
ProjectRole newRole = projectRoleManager.getProjectRole(PROJECT_OWNER);
//get project object
def project = projectManagerClass.getProjectObjByName(PROJECT_NAME);
//get the custom fieldsfield 
def cityFieldnotesField = getFieldByName("City"NOTES);
def//check brandFieldif = getFieldByName("Preferred Brand")

projects.each{
    the logged in user belongs to a role in a project
def projectRoles = projectRoleManager.getProjectRolesisUserInProjectRole(loggedInUser, it) newRole, project);
    //conditions to hide and show custom field based on project role
    if(projectRoles.findequals(){it.getName() == "Administrators"})NOTES_VISIBLE)) {
        isAdmin = true    notesField.setHidden(NOTES_NOT_VISIBLE);
    } else {
     } }  if(isAdminnotesField.equalssetHidden(true)) {
cityField.setHidden(false)
brandField.setHidden(false)NOTES_VISIBLE);
    }
else { cityField.setHidden(true) brandField.setHidden(true) }

Learning:

  1. Knowledge on how script-runner listener works for both cloud and server

  2. Got to understand different scenarios for Jira based on which scripts were written

  3. Basic knowledge on groovy

  4. 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

  5. While setting the value for single select system fields we can directly give “values” instead of “id’s of values”

  6. script-runner behaviors

  7. 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)

...