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 votes
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 Join to answer
Community showcase
Sarah Schuster
Posted Jan 29, 2018 in Jira

What are common themes you've seen across successful & failed Jira Software implementations?

Hey everyone! My name is Sarah Schuster, and I'm a Customer Success Manager in Atlassian specializing in Jira Software Cloud. Over the next few weeks I will be posting discussion topics (8 total) to ...

2,945 views 12 18
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
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot