How to update issue after getting value from dialog box using script runner

Muhammad Ramzan(Atlassian Certified Master)
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.
January 18, 2020

Dear Experts,

 

My scenario is 

  • I have created a button on issue
  • When user click on the button "update issue", system shows a dialog box , to confirm
  • Once your said yes, then update the issue with default parameters(i am not explaining here as there so many

Till here i am done and its working.

 

I am using script runner rest end point and javascript to get the value from  if they said yes.

 I have a groovy method which is updating the field after receiving the key.

I am stuck here how to proceed further, how can i execute the groovy method inside the javascript, once user said proceed.

1 answer

1 accepted

0 votes
Answer accepted
Peter-Dave Sheehan
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.
January 20, 2020

Hi @Muhammad Ramzan(Atlassian Certified Master) 

 I have a groovy method which is updating the field after receiving the key.

Is this groovy method available as a rest endpoint?

If so, you could make an ajax call from your dialog box javascript to that rest endpoint and provide the issue key as a parameter.

 Here is a recap of scriptunner configuration you will likely need:

  1. GET rest endpoint to serve the html of the dialog2 element
  2. POST rest endpoint to receive issue key and perform actions on the issue
  3. Web Item fragment to display the button that will call GET 
  4. Web Item resource to hold the Javascript used by the dialog2 and trigger an AJAX POST action when the confirm button is clicked (optional, you could also just embed your js in #1)
Muhammad Ramzan(Atlassian Certified Master)
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.
January 20, 2020

@Dear @Peter-Dave Sheehan , many thanks for the solution. I found the solution and did the same as you suggested here. Its working like a champ. many thanks,

Kay Domschke September 14, 2022

Hello, 

we have a similar challange. We are able to fill the fields back from dialog to Jira, but we struggle to make an Issue.Update Event. 

 

May your share how you solved it`?

Peter-Dave Sheehan
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.
September 14, 2022

You should not have to raise an event manually.

There are 3 ways I'm aware of for updating fields (in order of recommended preference)

  1. IssueService.update(user, validationResult, EventDispatchOption, sendMail)
    • This will generate the event specified in EventDisapatchOption
    • It will also add the change to issue history
    • It will automatically take care or re-indexing the issue
  2. IssueManager.updateIssue(user, issue, EventDisapatchOption, sendMail)
    • This will do all the same as the above except the indexing. Indexing has to be triggered manually
  3. CustomField.updateValue(fieldLayout, issue, ModifiedValue, changeHolder)
    • It's a low-level API that just changes the db value for the custom field.
    • This may or may not update the issue history. I haven't experimented with this in a while.
    • But there is certainly no event and not indexing taking place

If you use the first 2, you can specify to raise an IssueUpdated event.

Kay Domschke September 14, 2022

we checkd out everything, won't work.

Here is our Rest EndPoint Relay:

 

import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.issue.index.IssueIndexingService
import com.atlassian.jira.issue.UpdateIssueRequest
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate
import groovy.transform.BaseScript
import javax.ws.rs.core.MultivaluedMap
import javax.ws.rs.core.Response
import javax.ws.rs.core.Response.Status
import groovy.transform.Field
import groovy.json.JsonSlurper
import static groovyx.net.http.Method.*
import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate
import groovy.transform.BaseScript
import javax.ws.rs.core.MultivaluedMap
import javax.ws.rs.core.Response


@BaseScript CustomEndpointDelegate delegate

String issueKey = null
String BuildStatusValue = null    

BuildStatusRelay(httpMethod: "POST") {
    MultivaluedMap queryParams,
    def payload ->
        def jsonSlurper = new JsonSlurper()
        def content = jsonSlurper.parseText(payload)
        issueKey = content.key
        BuildStatusValue = content.body
        def im = ComponentAccessor.getIssueManager()
        def issueIndexingService = ComponentAccessor.getComponent(IssueIndexingService)
        MutableIssue issue = im.getIssueObject(issueKey)  
        def cfm = ComponentAccessor.getCustomFieldManager()
        def customField = cfm.getCustomFieldObject(24970)
        UpdateIssueRequest updateIssueRequest = UpdateIssueRequest.builder().eventDispatchOption(EventDispatchOption.ISSUE_UPDATED).sendMail(false).build()

      
        customField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(customField), BuildStatusValue),new DefaultIssueChangeHolder())
        def user = ComponentAccessor.getUserManager().getUserByName("kdomschk")
        //im.updateIssue(user, issue, EventDispatchOption.ISSUE_UPDATED, false)
        im.updateIssue(user, issue, UpdateIssueRequest.builder().eventDispatchOption(EventDispatchOption.ISSUE_UPDATED).sendMail(false).build())
        im.updateIssue(user, issue, updateIssueRequest)
        issueIndexingService.reIndex(issue)
        //IssueService.update(user, validationResult, EventDispatchOption, sendMail)
       
    Response.ok(payload).build()
   
}

public class Data {
    String customFieldID
    String value
    String issueKey  
}
Peter-Dave Sheehan
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.
September 15, 2022

Try this:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.index.IssueIndexingService
import com.atlassian.jira.util.ImportUtils
import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate
import groovy.json.JsonSlurper
import groovy.transform.BaseScript

import javax.ws.rs.core.MultivaluedMap
import javax.ws.rs.core.Response

@BaseScript CustomEndpointDelegate delegate

String issueKey = null
String BuildStatusValue = null

BuildStatusRelay(httpMethod: "POST") { MultivaluedMap queryParams, String payload ->
def jsonSlurper = new JsonSlurper()
def content = jsonSlurper.parseText(payload)
issueKey = content.key
BuildStatusValue = content.body
def im = ComponentAccessor.issueManager
MutableIssue issue = im.getIssueObject(issueKey)
def user = ComponentAccessor.getUserManager().getUserByName("kdomschk")
def cfm = ComponentAccessor.getCustomFieldManager()
def customField = cfm.getCustomFieldObject(24970)

issue.setCustomFieldValue(customField, BuildStatusValue)
im.updateIssue(user, issue, EventDispatchOption.ISSUE_UPDATED, false)

def issueIndexingService = ComponentAccessor.getComponent(IssueIndexingService)
Boolean wasIndexing = ImportUtils.indexIssues
ImportUtils.indexIssues = true
issueIndexingService.reIndex(issue)
ImportUtils.indexIssues = wasIndexing

Response.ok(payload).build()

}

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events