How to set a date customfield or choice list custom field with the IssueInputParameters ?

Bastien
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.
April 23, 2013

Hi,

My request is very simple :

I want to set customfield values during a transition by groovy script. I'm able to do this with text customfield, like this :

IssueInputParameters issueInputParameters = new IssueInputParametersImpl();

issueInputParameters = issueInputParameters.addCustomFieldValue("customfield_13700","All Closed");
issueInputParameters = issueInputParameters.addCustomFieldValue("customfield_12704",issue.getCustomFieldValue(customFieldManager.getCustomFieldObject("customfield_12704")));

This code works for text customfields, but not for date or choice list customfields.
I heard that for choice list customfields, I have to use the option ID, but I have not tried yet.

Is anyone can help me ?

Thank you.

10 answers

1 accepted

0 votes
Answer accepted
Bastien
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.
April 23, 2013

This code should works but it doesn't :


SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
date = sdf.format(issue.getCustomFieldValue(customFieldManager.getCustomFieldObject("customfield_10203")))
issueInputParameters = issueInputParameters.addCustomFieldValue("customfield_10203",date);

Does anyone know what is the ".0" after "2013-04-24 00:00:00" ? it seems to be milliseconds number, but event with

SimpleDateFormat("yyyy-MM-dd hh:mm:ss.S") or SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS"), it does not work.

Henning Tietgens
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.
April 23, 2013

You should try the format which is displayed in JIRA while you edit a date field.

Bastien
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.
April 23, 2013

It works, thank you Henning and Jamie ;)

SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy") (for me)

Juergen Krieger
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 17, 2016

as by this works, I do not recommend it.

As it does not take care of different time zones and therefore will be very creative when storing the date fields.

JamieA
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 18, 2016

date fields don't have a "time" component, so it's not really important. If you're adding 7 days to a date, the timezone is not relevant.

3 votes
Luboš Král June 5, 2014
DateTimeFormatter formater = this.dateTimeFormatterFactory.formatter().forUser(user);
Date dueDate = new Date();
String dueDateFormated = formater.format(dueDate); issueImput.Parameters.setDueDate(dueDateFormated);

Erik Buchholz
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.
March 4, 2021

Hi Lubos,

I get error with your code. Maybe I'm missing an import or a base script declaration?

dateTimeFormatterFactory is declared in a dynamic context, but you tried to access it from a static context.

When I change it to

DateTimeFormatter formatter = ComponentAccessor.getComponent(DateTimeFormatter)
DateTimeFormatter userFormatter = formatter.forUser(user)
Date myDate = new Date()
String dateFormatted = userFormatter.format(myDate)

I get the output format like "Just now" but not the input format for the date picker fields. Please check my answer for use of DateFieldFormat.

1 vote
Bastien
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.
April 23, 2013

Hi Henning,

Thank you for your help.

I found for select customfield, with this code :

issueInputParameters = issueInputParameters.addCustomFieldValue("customfield_10205",issue.getCustomFieldValue(customFieldManager.getCustomFieldObject("customfield_10205")).getOptionId().toString());

For the date issue, I tried to get a date customfield value and to cast it to String, but it didn't work ...


JamieA
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.
April 23, 2013

A cast won't work, use a SimpleDateFormat to convert to a string. But you may need to check the default date format that your jira uses. There should be a simpler way, but I don't know what it is.

Alex Gallien November 4, 2018

Jumping into an old thread in case it helps someone in the future. The way I was ultimately able to work around this was by ending my code with:

def update = issueService.validateUpdate(user, issue.id, issueInputParameters)

log.info(update.getErrorCollection().toString())

if (update.isValid()) {
issueService.update(user, update)
}

 

this returned

INFO : Errors: {customfield_27220=Invalid date format. Please enter the date in the format "dd/MMM/yy h:mm a".}
Error Messages: []

So I knew I needed to use SimpleDateFormat("dd/MMM/yy h:mm a") for my date. Hope this helps someone!

1 vote
Henning Tietgens
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.
April 23, 2013

For issueInputParameters you need a String representation of the content of the field. So for a select customfield you have to get the value of the option.

issueInputParameters = issueInputParameters.addCustomFieldValue("customfield_12705",issue.getCustomFieldValue(customFieldManager.getCustomFieldObject("customfield_12705")).getValue());

For dates I think you have to have a valid String representation of a date for your system, but I'm not sure, I didn't tried that.

Henning

JamieA
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.
April 23, 2013

Yes... generally it matches what the browser sends. Examples for date and datetimes etc:

inputParameters.with {
	setOriginalAndRemainingEstimate("4d", "3d")
	setDueDate "18/Nov/12"
	setAssigneeId  adminUser.name
	setSummary "updated summary"
	setProjectId project.id
	setIssueTypeId issueType.id
	setReporterId adminUser.name

	addCustomFieldValue(myNumberField.idAsLong, 3 as String)
	addCustomFieldValue(firstDate.idAsLong, "3/Jan/2012")
	addCustomFieldValue(firstDateTime.idAsLong, "30/Jan/13 10:53 PM")
}

0 votes
Erik Buchholz
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.
March 4, 2021

Hey,

I know this question is quite old but I found that here is missing a good solution since different Jira instances have different input formats and I worried that my code would break, if this input format will change at some day.

I found the DateFieldFormat class in the API: https://docs.atlassian.com/software/jira/docs/api/8.5.5/com/atlassian/jira/util/DateFieldFormat.html

With this I changed my code to

final DateFieldFormat dateFieldFormat = ComponentAccessor.getComponent(DateFieldFormat)
issueInputParameters.with {
addCustomFieldValue(myDatePickerField.idAsLong, dateFieldFormat.formatDatePicker(myDate))
}

I hope this helps anyone who is looking for a solution for this problem.

Regards
Erik

0 votes
Richard Bariny June 22, 2018

do not forget to set up 

issueInputParameters.setSkipScreenCheck(true) if target issue edit screen not contains customfield 
0 votes
Tom Jackson
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, 2013

Thanks for the tip Mladen!

I went a little rogue and added a TBD section on this page. I was hoping it would trigger an atlassian to do something about the information vacuum on this subject. Not a peep so far.

https://developer.atlassian.com/display/JIRADEV/Performing+Issue+Operations

0 votes
ThomasJ September 18, 2013

Has anyone figured out how to set a cascading select field in Jira 6?

I can set the parent option, but I can't figured out how to set both parent and child in one call.

This works to set the parent ...

issueInputParameters.addCustomFieldValue(myField.getIdAsLong(),"10637");

I've tried all of the following to set parent/child together, but to no avail:

issueInputParameters.addCustomFieldValue(myField.getIdAsLong(),"10637", "10640");

issueInputParameters.addCustomFieldValue(myField.getIdAsLong(),"10637, 10640");

issueInputParameters.addCustomFieldValue(myField.getIdAsLong(),"10637 10640");

issueInputParameters.addCustomFieldValue(myField.getIdAsLong(),"10637-10640");

issueInputParameters.addCustomFieldValue(myField.getIdAsLong(),"10637/10640");

Is this really not documented anywhere?

I am able to get this to work using customField.updateValue together with IssueManager.updateIssue using the code outlined by Eddie Web on this page: https://confluence.atlassian.com/pages/viewpage.action?pageId=163414052

But that seems old school...

Mladen Maravic September 24, 2013

This is how I managed to do just that (and lost 2 days figuring it out):

issueInputParameters.addCustomFieldValue("customfield_" + fieldId, parentOption.getOptionId().toString());
issueInputParameters.addCustomFieldValue("customfield_" + fieldId + ":1", childOption.getOptionId().toString());

0 votes
Bastien
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.
April 23, 2013

I didn't solve my problem for date custofield.

But I can show how I did for multiple select list ! Here is the code I use :


def varargsMethod(String ... args){
    args
}

potentialImpactOptions = issue.getCustomFieldValue(customFieldManager.getCustomFieldObject("customfield_10905"))
Set potentialImpactOptionsID = new HashSet();
if(potentialImpactOptions){
    for(Option option : potentialImpactOptions){
	   temp = option.getOptionId().toString();
	   potentialImpactOptionsID.add(temp);
    }
}
def potentialImpactArray = varargsMethod( potentialImpactOptionsID as String[] );
issueInputParameters = issueInputParameters.addCustomFieldValue("customfield_10905",potentialImpactArray);

0 votes
Bastien
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.
April 23, 2013

When I get the date customfield value, it returns this format : "2013-04-24 00:00:00.0"

Suggest an answer

Log in or Sign up to answer