How do I set a text customfield to a custom value

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

Hey,

check this plugin:

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

You can update CF using a post-function.

Cheers

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?

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();

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.

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?

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?

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?



Suggest an answer

Log in or Join to answer
Community showcase
Sarah Schuster
Posted Jan 29, 2018 in Jira

What are common themes you've seen across successful & failed Jira Software implementations?

Hey everyone! My name is Sarah Schuster, and I'm a Customer Success Manager in Atlassian specializing in Jira Software Cloud. Over the next few weeks I will be posting discussion topics (8 total) to ...

2,864 views 12 18
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot