Check Field is available for a given Issue Type using ScriptRunner Cloud

I want to check that a particular field is available for a given Issue Type (called Task), however this is proving tricky.

I am using the api /rest/api/2/issue/createmeta which should return a list of fields and their meta data.

For simplicity, I've written a script that checks that the field 'summary' is available (since I know it always will be, and is in fact the first field returned).  My script is:

 

def issueMeta = get('/rest/api/2/issue/createmeta?projectKeys=XXX&issuetypeNames=Task&expand=projects.issuetypes.fields')
.asObject(Map)
assert issueMeta.status == 200
def versionField = issueMeta.body.projects.issuetypes.fields.find { it.key == "summary"}
logger.info("version is ${versionField}")

The logger is returning "version is null".

Any help to work out what I'm doing wrong would be very much appreciated.

1 answer

1 accepted

Hi Greg,

I think the problem is that issueMeta.body.projects.issuetypes is an array, so you need to select the first element (or search for the element that matches the required issue type name).

def issueMeta = get('/rest/api/2/issue/createmeta?projectKeys=XXX&issuetypeNames=Task&expand=projects.issuetypes.fields')
.asObject(Map)
assert issueMeta.status == 200

def versionField = issueMeta.body.projects.issuetypes.find { it.name == 'Task' }.fields.find { it.key == "summary"}
logger.info("version is ${versionField}")

I hope that helps,

Jon

Thanks, that was indeed the problem. Strictly speaking 'projects' is an array too, so the working line appears to be

def versionField = issueMeta.body.projects.find { it.key == 'XXX' }
        .issuetypes.find {it.name == "Task"}
        .fields.find {it.key == "summary"}

Suggest an answer

Log in or Sign up to answer
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Amir Kazemi
Posted 3 hours ago in Jira

We want to know what Jira Service Desk apps you're using!

Hi Community! My name is Amir and I’m on the Jira Service Desk product marketing team at Atlassian. Our team would love to understand how you’re leveraging our ecosystem for Jira Service Desk. Wha...

17 views 0 4
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you