First up, I'm not a developer; the code below is hacked from examples found online.
Problem: We have a custom field to denote the version the fix actually shipped in ("Shipped Version"). It uses the Version pick-list. For compliance reasons, we want to restrict the options available at the Resolve transition. To achieve that we'd like to have a validator which would ensure only a couple of values are valid during a particular transition (e.g. error if they select something else).
I've tried writing the following code, but it's not picking up the selection when they select the correct value.
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.CustomFieldManager;
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
CustomField customField = customFieldManager.getCustomFieldObject("customfield_13978");
def currentValue = issue.getCustomFieldValue(customField);
if( (currentValue != 'Current' ) && (currentValue != 'Current-2' ) ) {
return false
}
else {
return true
}
I'm wondering if this is because the value of the version isn't stored as a string and thus won't compare properly. However, I'm not able to figure out how to convert it to a string or add the proper non-string id for the release into the compare (I tried entering the version ID number).
Thank you in advance for any help here!
-Brian
Thank you for your reply and assistance. I'm getting an error when I try this "Cannot find matching method"
Our field is a multi-value version selector. Could that be the cause of the error?
Both single and multi value version selector returns an array. And the code that I sent you is tested.
Can you share the whole code and the error you're getting?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thank you for your follow-up. Here is the code and error message:
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.CustomFieldManager;
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
CustomField customField = customFieldManager.getCustomFieldObject("customfield_13978");
def currentValue = issue.getCustomFieldValue(customField);
if (['R12-Current', 'ml-current'].contains(currentValue[0].name)) {
return true;
} else {
return false;
}
The error message I'm getting is below. It's on the line containing the IF statement:
Thank you again for your help and interest!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hello,
I think you're getting this problem if the field value is empty. If so you should add a null check as below. Or you can change accordingly. I mean if there's no version selected you can return false or true
if (null == currentValue) return false;
if (['R12-Current', 'ml-current'].contains(currentValue[0].name)) {
return true;
} else {
return false;
}
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.
Thank you @Tuncay Senturk _Snapbytes_ for your help! I really appreciate you pointed me in the right direction.
I was still getting an error with the update. I reached out to one of my Devs internally and after a bit of research we ended up with the code below.
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.project.version.Version;
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
CustomField customField = customFieldManager.getCustomFieldObject("customfield_13978");
def currentValue = issue.getCustomFieldValue(customField);
if (null == currentValue) return false;
def curVersionsList = currentValue.collect { ( it as Version ).getName() };
for( String ver : curVersionsList ) {
if (['R12-Current', 'ml-current'].contains( ver ) ) {
return true;
}
}
return false;
Thank you!
-Brian
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hello Brian,
Version Picker returns multiple version info. If your version picker is of single select type then you can try below code:
if (["Current", "Current-2"].contains(currentValue[0].name)) {
return true;
} else {
return false;
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hello Brian,
Version Picker returns multiple version info. If your version picker is of single select type then you can try below code:
if (["Current", "Current-2"].contains(currentValue[0].name)) {
return true;
} else {
return false;
}
I hope it helps
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.