Script fragment condition

arama mihai
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.
August 10, 2021

Hello,

 

I am trying to hide the "Create sub-task" button on a specific project, if the user performing the action (current user) is not in a specific user picker custom field.

What I have so far: 

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.user.ApplicationUser;
import com.atlassian.jira.issue.Issue;


def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def resp = customFieldManager.getCustomFieldObjectsByName("Responsible")

String username = issue.getCustomFieldValue(resp).name

ApplicationUser user = ComponentAccessor.getUserManager().getUserByName(username);

if (!issue.getProjectObject().getKey() == "ProjectA") { return true

}

else {

boolean condition = (issue.getProjectObject().getKey() == "ProjectA" && currentUser != user.name)

log.error("condition : " + condition)

condition
}

 

 My problem is that it works for projectA (hides the sub-task create button from the "More" menu) if the current user is not the responsible, but for all other projects it is not showing the button anymore, like the if condition does not exist.

 

Please let me know your thoughts.

 

Thank you.

1 answer

1 accepted

1 vote
Answer accepted
Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 10, 2021

Hi @arama mihai

For your requirement, you could try something like this:-

import com.atlassian.jira.component.ComponentAccessor

def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser

if(issue != null && jiraHelper.project?.key == "TPA" && loggedInUser.name == "ram") {
false
} else {
true
}

Please note, this sample code is not 100% exact to your environment. Hence, you will need to make the required modifications.

This is a working code that I have tested in my environment. Basically, if the currently logged-in user is ram, the user cannot specifically create sub-tasks for the TPA project. Other users, on the other hand, will be able to create the sub-tasks. For other projects, this user will be able to create the sub-tasks.

I hope this helps to answer your question. :)

Thank you and Kind Regards,

Ram 

arama mihai
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.
August 11, 2021

Hi @Ram Kumar Aravindakshan _Adaptavist_ ,

Thank you, that was very helpful for my final solution:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.ApplicationUser;

def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def resp = customFieldManager.getCustomFieldObjectsByName("Responsible")
def coord = customFieldManager.getCustomFieldObjectsByName("Coordinator")

if (jiraHelper?.project?.key == "abc") {

String usernameResp = issue.getCustomFieldValue(resp).name
String usernameCoord = issue.getCustomFieldValue(coord).name

ApplicationUser user1 = ComponentAccessor.getUserManager().getUserByName(usernameResp);
ApplicationUser user2 = ComponentAccessor.getUserManager().getUserByName(usernameCoord);
def approvedUsers = [user1.name, user2.name]

currentUser.name in approvedUsers
}
else {true}

Suggest an answer

Log in or Sign up to answer