issueService.validateCreate fails when creating an issue with REQUIRED custom field

I have a REQUIRED single select customfield set for a given screen which is mapped to a given issue in JIRA.(the one i will be creating programmatically). The value to be set for these fields is obtained from another dropdown in a transition screen.
When I try to create the issue programmatically using a jira plugin, it fails probably because a value is REQUIRED for the customfield(despite setting a default value for the field in JIRA) . If I dont make the field REQUIRED, i am able to create the issue and also update its value using
customField.updateValue() after creating the issue.

But I want to keep the REQUIRED option else I cannot remove the None option from the dropdown.
Ive tried setting the value using the below code before the issue is created.

issueInputParameters.addCustomFieldValue(custFieldId,"value to set");
UpdateValidationResult update = issueService.validateUpdate(ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser(), issue.getId(), issueInputParameters); 

if (update.isValid()) {
  issueService.update(ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser(), update);
  System.out.println("Update is valid"); //It prints this
}else{	 
  System.out.println("Update is not valid");
}
IssueLinkManager issueLinkManager = ComponentAccessor.getIssueLinkManager();
CreateValidationResult createValidationResult = issueService.validateCreate(user, issueInputParameters);

but createValidationResult fails and the issue is not created. Where am I going wrong ? Ive also tried using
issueInputParameters.setSkipScreenCheck(true) but with no luck.
Can anyone please help me to create the issue keeping the REQUIRED field option set.

For a single select custom field, the value you set is the id of the option, not its value. Here’s how the option id and option value are retrieved from a custom field option:

/**
 * The ID of the option or null if it currently does not have one.
 *
 * @return the ID of the option or null if it does not have one.
 */
@Nullable
Long getOptionId();

/**
 * The current value of the option. This is the option displayed to the user.
 *
 * @return the value of the option.
 */
String getValue();

Also remember that the options on one custom field have different ids from the options on another custom field, even if the values of those options are the same.

Also, is your custFieldId a Long or a String? If it’s a String, I believe you’ll need it to be specified in long form: e.g. "customfield_10000".

Since addCustomFieldValue takes the value as a String, convert the id of the custom field option to a string thus: String.valueOf(optionId).

1 Like

Hi David,

Thanks for your reply.

Just to make things a little more clearer in case I missed describing the issue properly.

I have a REQUIRED single select custom field called Change Type which is set on a transition screen and the same field is configured to be present on the View/Edit/Create screen of the type API Change . So I basically need to create the issue of type API Change with this field whose value I get from the transition screen.

Anyway, coming to setting the option Id

String.valueOf(optId) prints a no like 14508

[from inspecting the UI element in chrome, i got this
<option value="14508">Add (Adding new method)</option>],
so the option Id value obtained is correct.

Now I call ,

issueInputParameters.addCustomFieldValue("customfield_14901",String.valueOf(optId));
CreateValidationResult createValidationResult = issueService.validateCreate(user, issueInputParameters);
IssueResult createResult = issueService.create(user, createValidationResult);

customfield_14901 is the custom field Id of Change Type

But even then issue creation fails :disappointed:

java.lang.IllegalArgumentException: You can not create a null issue.
at com.atlassian.jira.bc.issue.DefaultIssueService.create(DefaultIssueService.java:181)
at com.atlassian.jira.bc.issue.DefaultIssueService.create(DefaultIssueService.java:163)

Cant seem to figure out where I am going wrong now. If I remove setting the custom field value and remove it from the API Change screen, then issue creation happens properly.

You should check the validity of the CreateValidationResult before passing it to the create method. There’s an isValid() method for that purpose, and also a getErrorCollection() method.

The JIRA source code reveals that the issue reference inside the CreateValidationResult is set to null if there are errors found during validation.

2 Likes