Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

PUT API call via ScriptRunner script throws MissingMethodException

Hello all,
we are currently populating an external system with the Jira ID via a put call. We need this to be able to send updates from this system to the respective Jira issue.

Thus, when creating the issue via workflow, we want to trigger a script runner script which will then perform the put post. However, we are currently still getting an error message, the body is intentionally empty for now for testing purposes. The endpoint differs to a field value which is filled by the creation, this is happening via post call from this external system. 

I suspect the syntax of my script does not fit.

package utils
import groovy.json.JsonOutput
import groovyx.net.http.ContentType
import groovyx.net.http.HttpResponseDecorator
import groovyx.net.http.RESTClient
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.component.ComponentAccessor

public class FreshservicePost{

public static String postResponse(Issue issue)

    {

def hostUrl = "https://markant-fs-test.freshservice.com/"

def customField = ComponentAccessor.customFieldManager.getCustomFieldObject("customfield_12727")

def strErgebnis = issue.getCustomFieldValue(customField).toString()

def endpointAndQuery = "https://markant-fs-test.freshservice.com/itil/changes/" +strErgebnis+".json"

// Turn the data will be sent into JSON, the HTTP requests specify in the header to accept application/JSON

// If the API accepts different types then you can change this and format the data accordingly

def bodyJson = JsonOutput.toJson([data: "{

    "itil_change": {

        "custom_field_attributes": {

           "external_jira_id_404134": "123"

        }

    }

}"])

//def put(def hostUrl, def endpointAndQuery, def bodyJson) {

    def client = new RESTClient(hostUrl)

    client.setHeaders([

        'Accept'       : ContentType.JSON,

        // If authentication mechanism of the API is Basic Authentication, 'Authorization' header with username and password encoded as Base 64 can be specified here

        'Authorization'"Basic ${'gPClHSQlxwCDf08NXTC:x'.bytes.encodeBase64()}"

    ])

    client.handler.success = { HttpResponseDecorator responsejson ->

        json

    }

    client.handler.failure = { HttpResponseDecorator response ->

        // Failure can be handled here

        log.error response.entity.content.text

        [:]

    }

    client.put(

        path: endpointAndQuery,

        contentType: ContentType.JSON,

        body: bodyJson

    )

//}

    }

    }

Here is the error:

2021-12-02 09:51:19,519 ERROR [workflow.AbstractScriptWorkflowFunction]: Workflow script has failed on issue CMTM-6567 for user 'tspraul'. View here: https://www.mnet.markant.de/jiraTest/secure/admin/workflows/ViewWorkflowTransition.jspa?workflowMode=live&workflowName=Markant+CMTM+Workflow+v1.1&descriptorTab=postfunctions&workflowTransition=191&highlight=6
groovy.lang.MissingMethodException: No signature of method: utils.FreshservicePost.main() is applicable for argument types: ([Ljava.lang.String;) values: [[]]
Possible solutions: wait(), wait(long), find(), any(), wait(long, int), any(groovy.lang.Closure)
at com.onresolve.scriptrunner.runner.RunScriptInvoker$run.call(Unknown Source)
at com.onresolve.scriptrunner.runner.AbstractScriptRunner.runScriptWithRawBindings(AbstractScriptRunner.groovy:196)
at com.onresolve.scriptrunner.runner.AbstractScriptRunner.runScript(AbstractScriptRunner.groovy:190)
at com.onresolve.scriptrunner.runner.AbstractScriptRunner$runScript$11.callCurrent(Unknown Source)
at com.onresolve.scriptrunner.runner.AbstractScriptRunner.runScriptAndGetContext(AbstractScriptRunner.groovy:216)
at com.onresolve.scriptrunner.runner.AbstractScriptRunner$runScriptAndGetContext$9.callCurrent(Unknown Source)
at com.onresolve.scriptrunner.runner.AbstractScriptRunner.runScriptAndGetContext(AbstractScriptRunner.groovy:301)
at com.onresolve.scriptrunner.runner.AbstractScriptRunner$runScriptAndGetContext$2.callCurrent(Unknown Source)
at com.onresolve.scriptrunner.runner.AbstractScriptRunner.runScript(AbstractScriptRunner.groovy:311)
at com.onresolve.scriptrunner.runner.ScriptRunner$runScript$12.call(Unknown Source)
at com.onresolve.scriptrunner.canned.jira.utils.TypedCustomScriptDelegate.execute(TypedCustomScriptDelegate.groovy:19)
at com.onresolve.scriptrunner.canned.jira.utils.TypedCustomScriptDelegate$execute$0.call(Unknown Source)
at com.onresolve.scriptrunner.canned.jira.workflow.postfunctions.CustomScriptFunction.execute(CustomScriptFunction.groovy:53)
at com.onresolve.scriptrunner.canned.jira.workflow.postfunctions.CustomScriptFunction$execute.callCurrent(Unknown Source)
at com.onresolve.scriptrunner.canned.jira.workflow.AbstractWorkflowCannedScript.execute(AbstractWorkflowCannedScript.groovy:23)
at com.onresolve.scriptrunner.canned.jira.workflow.AbstractWorkflowCannedScript$execute.call(Unknown Source)
at com.onresolve.scriptrunner.jira.workflow.AbstractScriptWorkflowFunction$_run_closure2.doCall(AbstractScriptWorkflowFunction.groovy:89)
at com.onresolve.scriptrunner.jira.workflow.AbstractScriptWorkflowFunction$_run_closure2.doCall(AbstractScriptWorkflowFunction.groovy)
at com.onresolve.scriptrunner.runner.diag.DiagnosticsManagerImpl$DiagnosticsExecutionHandlerImpl$_execute_closure1.doCall(DiagnosticsManagerImpl.groovy:370)
at com.onresolve.scriptrunner.runner.diag.DiagnosticsManagerImpl$DiagnosticsExecutionHandlerImpl$_execute_closure1.doCall(DiagnosticsManagerImpl.groovy)
at com.onresolve.scriptrunner.runner.ScriptExecutionRecorder.withRecording(ScriptExecutionRecorder.groovy:13)
at com.onresolve.scriptrunner.runner.ScriptExecutionRecorder$withRecording.call(Unknown Source)
at com.onresolve.scriptrunner.runner.diag.DiagnosticsManagerImpl$DiagnosticsExecutionHandlerImpl.execute(DiagnosticsManagerImpl.groovy:368)
at com.onresolve.scriptrunner.runner.diag.DiagnosticsExecutionHandler$execute$3.call(Unknown Source)
at com.onresolve.scriptrunner.jira.workflow.AbstractScriptWorkflowFunction.run(AbstractScriptWorkflowFunction.groovy:82)

 

Hopefully, somebody can help me!

Thank you in advance, 

Best regards!

1 answer

Hi!

I suppose you are doing this in a workflow postfunction or in an automation rule?

If so, try putting everything that is in the postResponse function in the script. The Issue context is already provided.

So your code would be:

import groovy.json.JsonOutput
import groovyx.net.http.ContentType
import groovyx.net.http.HttpResponseDecorator
import groovyx.net.http.RESTClient
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.component.ComponentAccessor

<code of the postResponse method here>


Let me know if this helps,

Jeroen

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Jira

Do you use Jira Cloud with Microsoft Teams?

Hi, Jira users! Do you use Jira alongside Microsoft Teams? We want to hear how you’ve used the power of Jira Cloud and Microsoft Teams (via the Jira Cloud for Microsoft Teams app) to achieve a team...

67 views 0 3
Read article

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you