Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Set field value on post-function (Scriptrunner)

Rafał Nowakowski September 24, 2018

Hey,

I need to copy value form last comment to my custom field (Simple multiline text field).

Below is my code, but it doesn't work. Part about last comment is okay. I think that setting value is wrong, any ideas why? I can't see any errors...

 

import com.atlassian.jira.component.ComponentAccessor

String lastcomment = ""

def comments = ComponentAccessor.commentManager.getComments(issue)
if (comments) {
lastcomment = comments.last().body
}


def changeCf = customFieldManager.getCustomFieldObjectByName("MyCustolField1")
issue.setCustomFieldValue(changeCf, lastcomment)
 

  

1 answer

1 accepted

1 vote
Answer accepted
Daniel Yelamos [Adaptavist]
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 24, 2018

Hi Rafal!

If this is a postfunction, you need to save your change!

Let's say that I want to change the assignee of an issue in a postfunction. 

I would use something like this:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.MutableIssue

def issueManager = ComponentAccessor.getIssueManager()
MutableIssue issue = issueManager.getIssueByCurrentKey("JRA-1")
def userManager = ComponentAccessor.getUserManager()

def anuser = userManager.getUserByKey("anuser")
//A single user selected
issue.setAssignee(anuser)

//Update your issue, if you don't do this, you might not see the change.
issueManager.updateIssue(anuser, issue, EventDispatchOption.ISSUE_UPDATED, false)

Do say if this didn't help, or if I can help you further.

Cheers!
DY

Rafał Nowakowski September 24, 2018

Hey, that helps.

This is my final code:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder

def updateCustomFieldValue(issue, String fieldName, String newValue, user) {
def customField = ComponentAccessor.customFieldManager.getCustomFieldObjectByName(fieldName)
issue.setCustomFieldValue(customField, newValue)
ComponentAccessor.issueManager.updateIssue(user, issue, EventDispatchOption.ISSUE_UPDATED, false)}

def getUserByName(userName) {
ComponentAccessor.userManager.getUserByName(userName)}

def user = getUserByName("tech")


String lastcomment = ""

def comments = ComponentAccessor.commentManager.getComments(issue)
if (comments) {
lastcomment = comments.last().body
}

updateCustomFieldValue(issue, "My field1", lastcomment, user)

Like 靳光光 likes this

Suggest an answer

Log in or Sign up to answer