Just a heads up: On March 24, 2025, starting at 4:30pm CDT / 21:30 UTC, the site will be undergoing scheduled maintenance for a few hours. During this time, the site might be unavailable for a short while. Thanks for your patience.
×Hi
I am running Jira Server v7.3.8
I am trying to write a custom rest end point that will set a custom fields value on an issue.
Here is my current code...
************************************************************************************************************************
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.config.PriorityManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.fields.rest.json.beans.JiraBaseUrls
import com.atlassian.jira.issue.fields.rest.json.beans.PriorityJsonBean
import com.atlassian.jira.issue.index.IssueIndexingService
import com.atlassian.jira.issue.index.IndexException
import com.atlassian.jira.issue.index.IssueIndexingParams
import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate
import groovy.transform.BaseScript
import org.codehaus.jackson.map.ObjectMapper
import groovy.json.JsonBuilder
import com.atlassian.jira.util.ImportUtils
import javax.ws.rs.core.MultivaluedMap
import javax.ws.rs.core.Response
import javax.ws.rs.core.Response.Status
log.setLevel(org.apache.log4j.Level.DEBUG)
@BaseScript CustomEndpointDelegate delegate
update_cf(
httpMethod: "PUT", groups: ["jira-administrators","automation"]
) {
MultivaluedMap queryParams, String body ->
def mapper = new ObjectMapper()
log.debug("Body Start \n " + body + "\n Body End")
def bean = mapper.readValue(body.getBytes(), Data)
assert bean.issueKey // must provide issue key
assert bean.customFieldID // must provide custom field ID
assert bean.value // must provide value
def im = ComponentAccessor.getIssueManager()
MutableIssue issue = im.getIssueObject(bean.issueKey)
if(issue == null){
return Response.serverError().entity([error: "Failed to Find issue with key ${bean.issueKey}"]).build()
}
log.debug("key : ${issue.getKey()}")
def cfm = ComponentAccessor.getCustomFieldManager()
def customField = cfm.getCustomFieldObject(bean.customFieldID)
if(customField == null){
return Response.status(Response.Status.BAD_REQUEST).build()
}
log.debug("CF : ${customField.getName()}")
log.debug("CF val : ${issue.getCustomFieldValue(customField)}")
issue.setCustomFieldValue(customField,bean.value)
IssueIndexingService issueIndexingService = ComponentAccessor.getComponent(IssueIndexingService.class)
if(issueIndexingService == null){
log.error("IssueIndexingService = null")
return Response.serverError().entity([error: "Failed to update index. Please notify a Jira Administrator"]).build()
}
log.debug("Starting try//catch")
log.debug(issue.getCustomFieldValue(customField))
try{
log.debug("starting reindex")
issueIndexingService.reIndex(issue);
log.debug("Issue should be reindexed")
log.debug("issue CF val : ${issue.getCustomFieldValue(customField)}")
}catch(Exception e){
log.error("Failed to Reindex")
return Response.serverError().entity([error: e.getMessage()]).build()
}finally{
log.debug("in Finally")
}
log.debug("Afer try//catch")
return Response.ok(new JsonBuilder(["package": bean]).toString()).build()
}
public class Data {
String customFieldID
String value
String issueKey
}
************************************************************************************************************************************************
Here is the log output......
2018-11-13 11:59:49,080 DEBUG [runner.ScriptRunnerImpl]: Body Start { "customFieldID" : "customfield_15446", "value" : "Yay it works", "issueKey" : "PSG-406792" } Body End 2018-11-13 11:59:49,087 DEBUG [runner.ScriptRunnerImpl]: key : PSG-406792 2018-11-13 11:59:49,087 DEBUG [runner.ScriptRunnerImpl]: CF : Last Name 2018-11-13 11:59:49,088 DEBUG [runner.ScriptRunnerImpl]: CF val : Maharaj 2018-11-13 11:59:49,088 DEBUG [runner.ScriptRunnerImpl]: Starting try//catch 2018-11-13 11:59:49,088 DEBUG [runner.ScriptRunnerImpl]: Yay it works 2018-11-13 11:59:49,088 DEBUG [runner.ScriptRunnerImpl]: starting reindex
response code = 200
body
{
"package": {
"value": "Yay it works",
"customFieldID": "customfield_15446",
"issueKey": "PSG-406792"
}
}
My confusion lies in that the code seems to stop running once it gets to the reindex part.
no exceptions, no finally, nothing after....
When I go to the issue the custom field also has not been updated.
The field is on the edit screen.
The user I am using to authenticate has permission to edit the issue.
Please can some one help
Try to use "updateValue" instead of setCustomFieldValue and use "updateIssue" instead of reindex.
Example of updateValue:
customField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(customField), customField,bean.value),new DefaultIssueChangeHolder());
Example of updateIssue:
im.updateIssue(user, (MutableIssue) issue, EventDispatchOption.ISSUE_UPDATED, false);
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
How to run this Rest API ?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.