Hello everyone,
I'll try to write a script with script runner in the Jira server.
I want to log in user has to check with the eRD-Leaders group and if the user exists the Project Plan field will be visible else Project Plan hide.
I wrote some codes but it's not working.
import com.atlassian.jira.component.ComponentAccessorgetFieldByName('Project Plan Delivery Date').setDescription('Select the date that the Project Plan will be delivered to PMT. Less than 60 days')def ProjectPlan = getFieldManager()def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()def group = ComponentAccessor.groupManager.getUserNamesInGroup("eRD-Leaders").contains("user")if ((user) == (group)){return getFieldByName('Project Plan Delivery Date')}else{getFieldByName('Project Plan Delivery Date').setHidden(true)}
Your logic goes wrong at the point where you define the "user" and "group" variables.
There's nothing wrong with getting either of them, but you've not looked at what they will actually return.
The code you've got will return
user: a user object
group: a true or false value, not a list of users. (true if the user is in the group)
So, your "if user == group" question is total nonsense. You're comparing a user with a boolean true or false.
You don't need to do that, your "group" variable already contains a true or false, so you should be saying "if (group == true)" (you could shorthand that to "if (group)"
The other problem is the use of "user" - your "def group =" is not looking through the group for a user, it's looking for a string. You need to change your "user =" to something that will get the user as a string, not a user object.
@Nic Brough -Adaptavist- Thank you for your answer
I didn't know these you said. I'm a noob in script!!
I used to think CSS or Javascript is hard but now I understand these script's models are ridiculous. I don't understand even one line!!
so I changed the user section. is it true?
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser().getUsername()
Also, I changed the if block but it's still not working.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Ok, not a coder, that's fine.
Is this script supposed to be a Behaviour or a calculated field? I ask because it looks like it has elements of both, and it's not clear what you are actually trying to achieve.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I wrote it in behavior.
I want to only the users who have login and member of the eRD-Leaders group could see the Project Plan field.
If those who have login don't member of the eRD-Leaders Project Plan field hide.
After this condition, if a user is a member of the eRD-Leaders they can't enter more than 60 days in the Project Plan field (it's a date picker). this is the second part and I didn't try this part because of the first condition.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I checked these lines
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @CRS123456 Try something different here :-
Instead of checking users in particular group, try to check logged in user groups , get all group name then apply condition.. User added in a group or not. If user is not added then hide field else field should be visible.
import com.atlassian.jira.component.ComponentAccessor
def groupManager = ComponentAccessor.groupManager
def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
final username = loggedInUser.username
def groupNames = groupManager.getGroupNamesForUser(username) as List
if (groupNames.contains("eRD-Leaders")) {
return "user in group"
getFieldByName('Project Plan Delivery Date').setHidden(false)
}else {
return "user not in group"
getFieldByName('Project Plan Delivery Date').setHidden(true)
}
Thanks
V.Y
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thank you
Are you sure about it?
It's not working. if my user is in eRD-Leaders or isn't, the field is still visible.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
finally, I found it.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Great! Glad to hear we are able help you in fixing the script :)
I have tested my code on my machine, it's working fine, i tested with attachment field not custom field andi have Added Script in Script Runner Behaviour >> Initialiser :-
import com.atlassian.jira.component.ComponentAccessor
def groupManager = ComponentAccessor.groupManager
def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def attachment = getFieldById("attachment")
final username = loggedInUser.username
def groupNames = groupManager.getGroupNamesForUser(username) as List
if (groupNames.contains("AF US Consumer User App Spt")) {
attachment.setHidden(false)
}else {
attachment.setHidden(true)
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks, @Vikrant Yadav
I can use these codes for other situations so it's good for those like me that don't know too much about scripts.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.