Script Runner create a subtask post function condition problem

Trying  to create a sub-task based on a checklist field.

I have the following condition:  cfValues['myfield']*.value.contains("myValue")

Returning the following error:  ERROR - No such property: cfValues for class: Script1

The static checker returns this:  The variable cfvalues is undeclared.

This worked on a server version I was previously using, but not on my current cloud version.

Did I do something wrong?

Thanks

2 answers

1 accepted

Accepted Answer
3 votes

Hi Alan,

There are significant differences between the Cloud and the Server versions of JIRA and how ScriptRunner works on the two platforms. You can read more here http://scriptrunner-docs.connect.adaptavist.com/jiracloud/ and here http://scriptrunner-docs.connect.adaptavist.com/jiracloud/migrating.html

For this particular question, the equivalent condition will look something like this:

// Get the field ids
def fields = get('/rest/api/2/field')
        .asObject(List)
        .body as List<Map>

def myFieldId = fields.find { it.name == "myfield" }.id
 
return issue.fields[myFieldId]?*.value.contains('myValue')

Hope that helps,

Jon

Hey Jon....sorry for the delay.

Using that script gives me the following error

2017-02-06 22:40:19,707 ERROR - Class: com.adaptavist.sr.cloud.workflow.CreateSubtask, Config: [className:com.adaptavist.sr.cloud.workflow.CreateSubtask, uuid:2a268983-c96a-4f4f-a42f-3e04804d17db, description:BitBucket Request, condition:// Get the field ids
def fields = get('/rest/api/2/field')
return issue.fields[myFieldId]?*.value.contains('bitbucket'), summary:New BitBucket Access Request!, issueTypeId:10202, executionUser:INITIATING_USER, additionalCode:]

Any thoughts on this?

Hi Alan,

Looks like I got my Groovy slightly wrong - it should be:

return issue.fields[myFieldId]*.value?.contains('myValue')

Thanks, Jon

Hey Jon:

Using this gives me a new error below:

groovy.lang.MissingPropertyException: No such property: issue for class: Script1 at Script1.run(Script1.groovy:8) at com.adaptavist.sr.cloud.events.ScriptExecution.run(ScriptExecution.groovy:22) at ScriptExecution1_groovyProxy.run(Unknown Source)

Static checker says:  "The variable 'issue' is undeclared" at line 8 column 8.

Thanks a lot for your time and help with this!

 

Hi Alan, are you running that from the Script Console or have you set it as the condition on a post function?

It won't work in the Script Console as the code expects there to already be an issue in the execution context...

Hey Jon:  I did run it from the console, however the static checker from the condition dialogue box is still saying "The variable 'issue' is undeclared" at line 8 column 8

I'll try to execute the post function anyways to see if it goes through.

Hi Alan, there are a couple of problems with the static type checking that we're close to having a fix for, so sometimes the messages are misleading. Did this code work in the post function?

Hello @Jon Bevan [Adaptavist] ,

 

I also have the same requirement, the above code is not working for us. Can you please help here?  I see the errors below : 

 

 

sc1.pngsc2.png

Thanks,

Swathi

Hi Swathi,

Those particular "errors" are actually just limitations of our type-checking system.

You can just ignore them, or make them go away by using the following code:

List<Map<String, Object>> fields = get("/rest/api/2/field")
    .asObject(List)
    .body
String myFieldId = fields.find { it.name == "myfield" }.id

return (issue.fields[myFieldId] as List<Map>)*.value?.contains('myValue')

Thanks,
Jon

Hi @Jon Bevan [Adaptavist] ,

 

Thanks so much it worked for a checklist custom field. 

I am trying the similar code on a single select list field "Department" but it is failing , can you tell me what Am I doing wrong?

def customFields = get("/rest/api/2/field")
.asObject(List)
.body
.findAll { (it as Map).custom } as List<Map>

def input1CfId = customFields.find { it.name == 'Department' }?.id
def input1 = issue.fields[input1CfId] as String
if (input1 == 'Operations & Maintenance')
{
return
}

 

When I run this , I expected a sub task to be created. I see that the run is successful with message the following message : 

2018-02-16 12:55:20.267 INFO - Serializing object into 'interface java.util.List'
2018-02-16 12:55:20.290 INFO - GET /rest/api/2/field asObject Request Duration: 555ms
2018-02-16 12:55:20.328 INFO - Condition didn't eval to true, exiting
 

Can you tell me why it din't create a sub task? Appreciate your help here. 

 

Thanks,

Swathi 

Hi Swathi,

I think you need to use:

def input1 = issue.fields[input1CfId].value as String

Thanks,
Jon

Hi @Jon Bevan [Adaptavist] ,

 

Appending value also dint help . However, when I tried some script in console i see that I am able to fetch the value... 

def issueKey = 'HR-39'

def result = get('/rest/api/2/issue/' + issueKey)
        .header('Content-Type', 'application/json')
        .asObject(Map)
if (result.status == 200){
    return result.body.fields.customfield_10088.value
}
else {

    return "Failed to find issue: Status: ${result.status} ${result.body}"
}

 

Same script is not working when I place in post function. Can you help me in converting the above code to work in post function? 

 

Thanks,

Swathi

Hi @Swathi Komirelli [Appfire]

The script will fail if there is no value in that field, so you'll need to use the safe navigation operator:

def input1 = issue.fields[input1CfId]?.value as String

If that doesn't resolve the problem then I'll need to see the specific error messages you're seeing.

Thanks,
Jon

PS - I have verified that the field that I am using DOES exist, and I have done a re-index for good measure.

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted Sep 25, 2018 in Jira

Atlassian Research Workshop opportunity on Sep. 28th in Austin, TX

We're looking for participants for a workshop at Atlassian! We need Jira admins who have interesting custom workflows, issue views, or boards. Think you have a story to sha...

449 views 7 5
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