Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

How do I update a CustomField using scriptrunner custom rest endpoints?

Robert de Groot
Contributor
November 13, 2018

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

2 answers

1 accepted

1 vote
Answer accepted
Neta Elyakim
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.
November 13, 2018

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); 
Robert de Groot
Contributor
November 13, 2018

Thank you this worked for me

Like • 2 people like this
0 votes
Sujitha
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
October 22, 2020

How to run this Rest API ? 

Suggest an answer

Log in or Sign up to answer
TAGS
atlassian, ace, atlassian community event, donation, girls who code, women in tech, malala fund, plan international, kudos, community badge, badge, atlassian badge, International Women’s month, International Women’s Day, women's month, women's day

10 for Change at Atlassian Community Events

Show up and give back by attending an Atlassian Community Event: we’ll donate $10 for every event attendee in March!

Join an Atlassian Community Event!
AUG Leaders

Upcoming Jira Events