Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Workflow Validator to check version in custom field

Brian Moudy February 28, 2019

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

3 answers

0 votes
Brian Moudy March 6, 2019

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?

Tuncay Senturk _Snapbytes_
Community Champion
March 6, 2019

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?

Brian Moudy March 8, 2019

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:

JIRACheck_ErrorMessage.pngThank you again for your help and interest!

Tuncay Senturk _Snapbytes_
Community Champion
March 8, 2019

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;
}
Tuncay Senturk _Snapbytes_
Community Champion
March 11, 2019

Hello @Brian Moudy 

Did that work?

Brian Moudy March 14, 2019

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 

0 votes
Tuncay Senturk _Snapbytes_
Community Champion
March 1, 2019

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;
}
0 votes
Tuncay Senturk _Snapbytes_
Community Champion
March 1, 2019

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

Suggest an answer

Log in or Sign up to answer