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

This widget could not be displayed.

Hey,

check this plugin:

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

You can update CF using a post-function.

Cheers

This widget could not be displayed.

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

This widget could not be displayed.

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?

This widget could not be displayed.

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 Sign up to answer
Atlassian Summit 2018

Meet the community IRL

Atlassian Summit is an excellent opportunity for in-person support, training, and networking.

Learn more
Community showcase
Posted Wednesday in New to Jira

Are you planning to trial, or are currently trialling Jira Software? - We want to talk to you!

Hello! I'm Rayen, a product manager at Atlassian. My team and I are working hard to improve the trial experience for Jira Software Cloud. We are interested in   talking to 20 people planning t...

106 views 2 0
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