Copy worklog to custom field

fjodors
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.
February 19, 2016

Hello

 

I am trying to copy logged work to specific customfield using Script listener with event "Issue Commented"

Here is my code

import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.comments.CommentManager
def customFieldManager = ComponentAccessor.getCustomFieldManager()
MutableIssue issueToUpdate = (MutableIssue) event.issue;
def timeWorked = issueToUpdate.getTimeSpent();
def cf = customFieldManager.getCustomFieldObjects(issueToUpdate).find {it.name == 'customfieldName'};
issueToUpdate.setCustomFieldValue(cf, timeWorked)

 

After comenting issue I see that listener was executed, however custom field value was not changed.

(the same problem is here: https://answers.atlassian.com/questions/35781060 )

 

Could you help me understand what is wrong? Is "setCustomFieldValue" correct function to update fields?

 

Thank you in advance,

Fyodor

2 answers

1 accepted

2 votes
Answer accepted
Jeremy Gaudet
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.
February 19, 2016

Instead of:

def cf = customFieldManager.getCustomFieldObjects(issueToUpdate).find {it.name == 'customfieldName'};
issueToUpdate.setCustomFieldValue(cf, timeWorked)

Try:

IssueManager issueManager = ComponentManager.getInstance().getIssueManager();
def cf = customFieldManager.getCustomFieldObjectByName('customfieldName');
issueToUpdate.setCustomFieldValue(cf, timeWorked);
issueManager.updateIssue(event.getUser(), issueToUpdate, EventDispatchOption.ISSUE_UPDATED, false);
fjodors
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.
February 21, 2016

Hello Jeremy, Thanos

 

Thank you advises.

Example from Thanos with "cf.updateValue(...)" works, but it doesn't fire update event, so I assume I need to use example with "issueManager.updateIssue(...)" function.

Unfortunately I am not able to put updateIssue method

How can I do it?

 

1) These examples returns error: "cannot find matching method getIssueManager"

IssueManager issueManager = ComponentManager.getInstance().getIssueManager();

or

def componentManager = ComponentManager.getInstance()
IssueManager issueManager = componentManager.getIssueManager()

 

2) This example returns error: "cannot find matching method updateIssue"

IssueManager issueManager = ComponentAccessor.getIssueManager();
issueManager.updateIssue(event.getUser(), issueToUpdate, EventDispatchOption.ISSUE_UPDATED, false);

 

Thanks,

Fyodor

Jeremy Gaudet
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.
February 21, 2016

What version of JIRA?

fjodors
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.
February 21, 2016

version 7.0.10

Jeremy Gaudet
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.
February 21, 2016

That "updateIssue" should work in JIRA 7, according to the javadoc; however, in hindsight, you also have to import "com.atlassian.jira.event.type.EventDispatchOption" so you can reference ISSUE_UPDATED.

fjodors
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.
February 21, 2016

Hello Jeremy,

 

thank you, now it works

1 vote
Thanos Batagiannis _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.
February 19, 2016

Hi Fjodors,

Assuming that your field is a text field, try update the value like this 

def timeWorked = issueToUpdate.getTimeSpent().toString()
def changeHolder = new DefaultIssueChangeHolder()
cf.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(cf), timeWorked),changeHolder)

Please let me know if you use a different type of custom field. Also it would be useful to know JIRA and SR versions.

Kind regards

Suggest an answer

Log in or Sign up to answer