fieldjirabehavior

Script to make custom field mandatory when issue type picked in JIRA bahaviours


I found this script to make a custom field mandatory when a specific issue type is chosen. The only issue I am having is that the field becomes mandatory only after you "update" the ticket. I am trying to get it to appear mandatory (red asterisk next to it) as soon as you change the issue type without updating as yet.

I am aware you can make the custom field mandatory with the field configurations but you would need to move issues between the different issue types as it is not available in the drop down.

Can someone assist to edit this code? I got it working with 2 custom fields but the issue type field is causing an issue.

def custfield = getFieldById("customfield_17914")

String issueType = getIssueContext().getIssueType().getName()
if (issueType == "Epic") 
{
custfield.setHidden(false);
custfield.setRequired(true);
} 

Thanks.


Solution

  • I guess you are trying to get the issue type in Behaviours.

    If you are trying to check current issue's issue type is Epic or not; you can use special variable called underlyingIssue in ScriptRunner Behaviours. Following code piece will give you the current issue's issue type:

    underlyingIssue.issueType.name
    

    Or, if the issue type is on the screen; you just can get its value:

    import static com.atlassian.jira.issue.IssueFieldConstants.*
    
    def issueTypeField = getFieldById(ISSUE_TYPE)
    
    def issueType = issueTypeField.getFormValue()
    

    So, if you get the issue type like that and also if you add the script in "Issue Type" field's change; you can make the custom field required while issue type is changing.