Issue update scriptrunner script works in console not with listener

Hey guys,

I have a script that is supposed to make a SOAP call to a different application when a ticket is updated and contains a certain value. Here is basically my script:

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField

String username = "username"
String password = "password"

//ID's for custom field and value
String customFieldId = "customfield_11300"
String customFieldValue = "[value1]"

CustomFieldManager customFieldManager = ComponentManager.getInstance().getCustomFieldManager();
CustomField customField = customFieldManager.getCustomFieldObject(customFieldId);
String fieldValue = issue.getCustomFieldValue(customField);

def soapRequest = """<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:DskTkt_CreateTicket">
   <soapenv:Header>
      <urn:AuthenticationInfo>
         <urn:userName>${username}</urn:userName>
         <urn:password>${password}</urn:password>
      </urn:AuthenticationInfo>
   </soapenv:Header>
   <soapenv:Body>
      <urn:create>
         <urn:Area>Services</urn:Area>
         <urn:CaseType>Service Request</urn:CaseType>
         <urn:Description>test</urn:Description>
         <urn:GroupID>2000</urn:GroupID>
         <urn:GroupIDTxt>2000</urn:GroupIDTxt>
      </urn:create>
   </soapenv:Body>
</soapenv:Envelope>"""

if(fieldValue == customFieldValue) {
   try {
      def soapUrl = new URL("soapdomain/services/?webService=CreateTicket")
      def connection = soapUrl.openConnection()
      connection.setRequestMethod("POST")
      connection.setRequestProperty("Content-Type" ,"text/html")
      connection.setRequestProperty("SOAPAction", "")
      connection.doOutput = true

      Writer writer = new OutputStreamWriter(connection.outputStream)
      writer.write(soapRequest)
      writer.flush()
      writer.close()
      connection.connect()

      def soapResponse = connection.content.text
      def Envelope = new XmlSlurper().parseText(soapResponse)

   } catch (Exception e) {}
}

If I post this into the script console it works great. I can go into the other application and see that it worked. I then went to setup the listener with 'Issue Update' and it doesn't work. I've modified the JIRA ticket several times selecting that custom field value with no luck. Am I missing something? Thanks for any help!

1 answer

1 accepted

0 vote
Henning Tietgens Community Champion Feb 20, 2014

You have to write the listener as a class, did you do that? See chapter "Custom Groovy Listeners (for advanced users)" on https://jamieechlin.atlassian.net/wiki/display/GRV/Listeners

If this doesn't help, I would suggest to add log messages to see where the problem is.

log.error "message"

will log the message as ERROR to your log files. If you want to use log.info or log.debug make sure the log level is set to the corresponding value.

Ok, this is all new to me. I currently have the file in classes/custom/listeners/soapCall.groovy. So I modified the script to be a class (it was not before hand).

After making these changes I went to re-add the listener and got this error:
Problem loading class: startup failed: /opt/atlassian/jira/atlassian-jira/WEB-INF/classes/custom/listeners/soapCall.groovy: 57: unexpected token: if @ line 57, column 4. if(fieldValue == customFieldValue) { ^ 1 error

At this point I'm wondering if I have been staring at the code too long. I don't see why I am getting that.

Henning Tietgens Community Champion Feb 23, 2014

Could you post the script? Otherwise it's very difficult to analyse the error. The error message says there is an "if" on a place where it's not expected. Did you close all brackets and strings?

It will only let me comment 2000 characters, so:

package custom.listeners

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.event.issue.IssueEvent;
import org.apache.log4j.Category

class soapCall extends AbstractIssueEventListener {
	Category log = Category.getInstance(soapCall.class)

<<<<<lines 5-53 from initial post>>>>>

	@Override
   void workflowEvent(Issue event) {
      log.debug "Event: ${event.getEventTypeId()} fired for ${event.issue} and caught by soapCall"
   }
}

Henning Tietgens Community Champion Feb 23, 2014

You have to put your source (lines 5-53) into the method workflowEvent() after the log.debug call.

Henning Tietgens Community Champion Feb 23, 2014

Some additional points:

  • add import com.atlassian.jira.event.issue.AbstractIssueEventListener
  • change type Issue to IssueEvent for first parameter of workflowEvent()
  • change
    String fieldValue = issue.getCustomFieldValue(customField)
    to
    String fieldValue = event.issue.getCustomFieldValue(customField)

Thanks! That appears to have done the trick!

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published yesterday in Jira

5 ways you can make the most of Jira Software and Bitbucket Cloud

As part of the Bitbucket product team I'm always interested in better understanding what kind of impact the use of our tools have on the way you work. In a recent study we conducted of software devel...

56 views 0 5
Read article

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