How do I set a text customfield to a custom value

e October 20, 2013

Jira 6.0.8. How do I set my own value to a custom field?

Is it going to be a post function?

PS: Later on, I am going to change Original Estimate field. So, I want to learn basing on text-based custom fields.

Thanks!

4 answers

0 votes
e October 22, 2013

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder


CustomField field1 = customFieldManager.getCustomFieldObject(new Long(15900));
String currentFieldValue = issue.getCustomFieldValue(field1);
String newFieldValue = "New value for this text field";
field1.updateValue(null, issue, new ModifiedValue(currentFieldValue, newFieldValue), new DefaultIssueChangeHolder());

This code (implemented as a post-function) does not change my CF value. It's a Text Field (single line).
 
Is it supposed to?



0 votes
John Bishop
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.
October 21, 2013

Using groovy, you could do something like this:

CustomField field1 = customFieldManager.getCustomFieldObject(new Long(11807));
String currentFieldValue = issue.getCustomFieldValue(field1);
String newFieldValue = "New value for this text field";
field1.updateValue(null, issue, new ModifiedValue(currentFieldValue, newFieldValue), new DefaultIssueChangeHolder());

You just need to replace 11807 in the first line to whatever the id of your field is.

e October 21, 2013

JohnBishop [CSG International]

Thank you for the code. But I can't make it work. Am I supposed to use it as a post-function script?

John Bishop
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.
October 21, 2013

You can use it as a post-function script or in a listener.

What problem are you having when you try to make it work?

0 votes
e October 21, 2013

I prefer to use some groovy, please. Can you show a sample source?

customfield_10901 - is of text type. So, how do I change its value?

deepali3031 July 11, 2017

Hi et,

I know this is a very old question but still I would like to provide a sample code to use in script listner on issue update event. In below code, 'Dev Time Estimate' is a text field where estimate is provided in the format  1m, 2w, 3d format. And the the 'Dev Time Estimate in hours' is numeric field this field is use in boards for estimation as board estimation is restricted to Text fields. So the below code converts the text field value into numeric value in terms of hours. Thanks!

import javax.mail.*;
import javax.mail.internet.*;
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.issue.ModifiedValue;
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder;
import com.atlassian.jira.issue.label.LabelManager;
import com.atlassian.jira.issue.label.Label;

MutableIssue currentIssue = (MutableIssue)event.getIssue();

def customFieldManager = ComponentAccessor.getCustomFieldManager();
def issueManager = ComponentAccessor.getIssueManager();

//Issue issue = issueManager.getIssueObject("ANT-1495" );

def devTimeEst = customFieldManager.getCustomFieldObjectByName("Dev Time Estimate");
def devTimeEstVal = event.issue.getCustomFieldValue(devTimeEst).toString();

int devTimeEstValLength = devTimeEstVal.length()
int timePosDev = 0;
Double hoursDev = 0

def hoursDevTimeEst = customFieldManager.getCustomFieldObjectByName("Dev Time Estimate_in hours");

// Calculate Dev Time Estimate
// If the value provide is a number, consider it to be an hour value
if (devTimeEstVal.isNumber()) {
hoursDev += (Double.valueOf(devTimeEstVal));
}

else {

for (int i = 0; i < devTimeEstValLength; i++) {
if (devTimeEstVal.charAt(i) == 'w') {
hoursDev += (Double.valueOf(devTimeEstVal.substring(timePosDev, i)) * 5 * 8);
if ((i + 1) < devTimeEstValLength) {
timePosDev = i + 2;
}
}

else if (devTimeEstVal.charAt(i) == 'd') {
hoursDev += (Double.valueOf(devTimeEstVal.substring(timePosDev, i)) * 8);
if ((i + 1) < devTimeEstValLength) {
timePosDev = i + 2;
}
}

else if (devTimeEstVal.charAt(i) == 'h') {
hoursDev += (Double.valueOf(devTimeEstVal.substring(timePosDev, i)));
if ((i + 1) < devTimeEstValLength) {
timePosDev = i + 2;
}
}

else if (devTimeEstVal.charAt(i) == 'm') {
hoursDev += (Double.valueOf(devTimeEstVal.substring(timePosDev, i)) * 60);
if ((i + 1) < devTimeEstValLength) {
timePosDev = i + 2;
}
}

else {
continue;
}

}
}

currentIssue.setCustomFieldValue(hoursDevTimeEst, hoursDev)

//update database
currentIssue.store();

0 votes
Christian Czaia _Decadis AG_
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.
October 20, 2013

Hey,

check this plugin:

https://marketplace.atlassian.com/plugins/com.googlecode.jira-suite-utilities

You can update CF using a post-function.

Cheers

Suggest an answer

Log in or Sign up to answer