Scriptrunner Listener Original Estimate Based On Custom Field

Justin December 9, 2019

Looking for some guidance as I'm stuck on my next steps...

My goal is to create a listener on a single project and when the "T-Shirt" custom field is set or modified (values are "S", "M", "L") we update the original estimate field with a corresponding value.

Small = 5d
Medium = 10d
Large = 15d

I've added a post function to test on new issue creation to set an original estimate, but still nothing is set (currently just testing the "M" size). 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.security.JiraAuthenticationContext
import com.atlassian.jira.ComponentManager

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def tshirt = customFieldManager.getCustomFieldObject(10142)
def tshirtsize = issue.getCustomFieldValue(tshirt)

if(tshirtsize =='M'){
MutableIssue issue=issue
issue.setEstimate ((long)288000)
IssueEvent event;
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
ComponentAccessor.getIssueManager().updateIssue(currentUser, issue, EventDispatchOption.ISSUE_UPDATED, false);
}
log.debug(tshirt);
log.debug(tshirtsize);

2019-12-10 00:18:42,530 DEBUG [workflow.ScriptWorkflowFunction]: T-Shirt
2019-12-10 00:18:42,530 DEBUG [workflow.ScriptWorkflowFunction]: M

Debug logs are showing the custom field and it's value correctly, so it's how I'm setting the original estimate field. Any help would be greatly appreciated.

1 answer

1 accepted

0 votes
Answer accepted
Nic Brough -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.
December 10, 2019

It is the "if" statement that is checking if the selection is M.

Select lists hold "options", not strings, so "if ( <option that is labelled as M> == 'M' )" is going to return a false.

I would usually do it this way:

if ( "M".equals(tshirtsize.getName() ) )

(I've had defensive coding kicked into me, and the use of .equals rather than == as it's safer for strings)

Justin December 12, 2019

Thank you very much for your response @nic .

I tried to update the script with your suggestion and using import com.atlassian.jira.issue.fields per the API documentation (https://docs.atlassian.com/software/jira/docs/api/7.6.1/com/atlassian/jira/issue/fields/Field.html#getName--) but it's still cannot find the matching method for tshirtsize.getname().

Can you point me in the right direction? 

Suggest an answer

Log in or Sign up to answer