I have a question about the script runner plugin.
Is it possible to check for a specific value in Security Level and if that value is present, make some other field required? I want to do this all in the create transition.
Thanks in advance.
Hi Marc,
You could do something similar to this:
import com.atlassian.jira.component.ComponentAccessor import com.atlassian.jira.issue.Issue import com.atlassian.jira.issue.security.IssueSecurityLevelManager import com.opensymphony.workflow.InvalidInputException def Issue issue = issue def IssueSecurityLevelManager issueSecurityLevelManager = ComponentAccessor.getIssueSecurityLevelManager() if (! issue.securityLevelId) return def securityLevel = issueSecurityLevelManager.getSecurityLevel(issue.securityLevelId) if (securityLevel.name == "level two" && ! issue.environment) { throw new InvalidInputException("environment", "You must enter an environment if you level two is your security level") }
Thanks all for the answers, i'm almost there, but now I can't get the customfield part working (Script runner noob here). To clearify: this is gonna be used in the validator part of the workflow.
So far I got this:
import com.atlassian.jira.issue.IssueManager; import com.atlassian.jira.ComponentManager; import com.atlassian.jira.component.ComponentAccessor; import com.atlassian.jira.issue.Issue; import com.atlassian.jira.issue.security.IssueSecurityLevelManager; import com.opensymphony.workflow.InvalidInputException; import com.atlassian.jira.issue.CustomFieldManager; import com.atlassian.jira.issue.fields.CustomField; IssueManager issueManager = ComponentManager.getInstance().getIssueManager(); def Issue issue = issueManager.getIssueObject( "TBS-14" ); //def Issue issue = issue CustomFieldManager customFieldManager = ComponentManager.getCustomFieldManager(); CustomField cf = customFieldManager.getCustomFieldObjectByName('Client') def IssueSecurityLevelManager issueSecurityLevelManager = ComponentAccessor.getIssueSecurityLevelManager(); if (! issue.securityLevelId) return def securityLevel = issueSecurityLevelManager.getSecurityLevel(issue.securityLevelId); if (securityLevel.name == "Intern" && ! issue.getCustomFieldValue(cf)) { throw InvalidInputException("Product", "Enter a product.") }
The error I'm getting is this (Script Console):
groovy.lang.MissingMethodException: No signature of method: static com.atlassian.jira.ComponentManager.getCustomFieldManager() is applicable for argument types: () values: [] Possible solutions: getCustomFieldManager() at Script62.run(Script62.groovy:18)
I guess the declaration of the getCustomFieldManager is not correct... Any ideas?
When i comment out the CustomFieldManager and the ComponentManager declaration, I get no error at the console (but it doesn't work at the workflow postfunction).
Marc
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
ComponentManager.getCustomFieldManager(); should be:
ComponentManager.getInstance().getCustomFieldManager()
or better:
ComponentAccessor.getCustomFieldManager()
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hey Marc,
If I understand you correctly you should create new Script Validator on desired transition, then retrieve from issue object its security level id via getSecurityLevelId and if it's level you are looking for and other field value is missing, then result in failed validation. You can look for inspiration in Jamies example: Require Fix Version if resolution is Fixed.
Regards,
Adam
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Adam,
Thanks for the answer. I've been struggling with it and I got this so far, but it's not validating when I choose security level Intern and product None:
import com.atlassian.jira.issue.Issue import com.opensymphony.workflow.InvalidInputException def Category log = Category.getInstance("com.onresolve.jira.groovy.PostFunction") Issue myIssue = issue if (myIssue.getSecurityLevelId() == "Intern" && myIssue.getCustomFieldValue("customfield_16100") == "None") { invalidInputException = new InvalidInputException("Kies een valide product.") }
Am I missing something?
Regards,
Marc
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Oh, that's due to getSecurityLevelId returning Long, not string, so you should find out what is id of your security level and put it in if statement.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
And to be sure - you use "log = Category.getInstance("com.onresolve.jira.groovy.PostFunction")", but it's part of Validation, not Post Function, right?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Nope, it does not affect how validation work. Did you try to replace "Intern" with proper id?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Yup, I did. So far I replaced "Intern" with the ID (10903 in this case) but no luck. Got this now: if (myIssue.getSecurityLevelId() == 10903 etc. He stills accepts the security level (Intern; ID 10903) with the option None for product
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Did you try to find out what exceptions are logged in JIRA log? It seems, that getCustomFieldValue accepts instance of CustomField type, not string. This should help as long as your customfield_16100's value returns string, which is quite probable.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
See Alejo's answer for how to look up the security level if you want to compare it by name. For your problem with custom fields get the custom field by using CustomFieldManager... lots of examples for this around
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.