Script Runner create a subtask post function condition problem

Alan Williams February 3, 2017

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

3 votes
Answer accepted
Jon Bevan [Adaptavist]
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 5, 2017

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

Alan Williams February 8, 2017

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?

Jon Bevan [Adaptavist]
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 9, 2017

Hi Alan,

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

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

Thanks, Jon

Alan Williams February 10, 2017

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!

 

Jon Bevan [Adaptavist]
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 10, 2017

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...

Alan Williams February 10, 2017

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.

Jon Bevan [Adaptavist]
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 12, 2017

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?

Deleted user February 15, 2018

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

Jon Bevan [Adaptavist]
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 15, 2018

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

Deleted user February 16, 2018

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 

Jon Bevan [Adaptavist]
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 20, 2018

Hi Swathi,

I think you need to use:

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

Thanks,
Jon

Deleted user February 20, 2018

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

Jon Bevan [Adaptavist]
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 20, 2018

Hi @[deleted]

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

0 votes
Alan Williams February 3, 2017

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