It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Looking for a script to fill customfield (multiple-checkboxes) in over 300 issues

Hello friends,

I must perform a action in over 300 issues to fill a customfield. I has write a user define servlet as plugin, to do it.

In over 300 issues I look for some other fields and keywords in description and write a arraylist with the values of my checkboxes. Now my problem is to assign the it to customfield of issue.

MutableIssue issue = ComponentAccessor.getIssueManager().getIssueObject("TEST-1")

ArrayList<String> opt=new ArrayList<String>();
opt.add("Value1");
opt.add("Value3");


CustomField f = ComponentAccessor.getCustomFieldManager.getFieldByName("mycheckboxfield")
issue.setCustomFieldValue(f,opt);

Why my last-line in code-Snipped doesn't assign the value to the issue?
(None exception or log-warnings was written.)

Know anyone a better way to reach this goal?

Thank you in advance for your spend time.

Best regards

4 answers

1 accepted

2 votes
Answer accepted

Hi,

I think you have to use an com.atlassian.jira.issue.customfields.option.Option object from the existing options of the multi-checkbox custom field fieldconfiguration as argument for setCustomFieldvalue().

If you want to use Strings as value you could use the IssueService.update() (https://developer.atlassian.com/display/JIRADEV/Performing+Issue+Operations).

For tasks like this I use the ScriptRunner Plugin, but maybe, the way Renjith suggested, using CLI, could be preferred.

Henning

Can you wirte a smal example-code, please?

If the sample is for CLI, you need to just read https://bobswift.atlassian.net/wiki/display/JCLI/Examples

jira --action updateIssue --issue "ZJIRACLI-4" --field "custom1" --values "xxxx"

Sorry ;)

IssueService issueService = ComponentAccessor.getIssueService();
IssueInputParameters issueInputParameters = new IssueInputParametersImpl();
issueInputParameters.addCustomFieldValue(cFO.getId(), "10017" );
UpdateValidationResult updateValidationResult = issueService.validateUpdate(ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser(), issue.getId(), issueInputParameters);
issueService.update(ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser(), updateValidationResult);

Is that correct so?

Whats the seperator for assign more than one optionfield-values?

String[] val=new String[2]; val[0]="10017"; val[1]="10018";
issueInputParameters.addCustomFieldValue(cFO.getId(), val );

work fine!

Try something like this within ScriptRunner:

import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueInputParameters
import com.atlassian.jira.issue.IssueInputParametersImpl

Issue issue
IssueService.UpdateValidationResult validationResult
IssueService.IssueResult updateResult

ComponentManager cm = ComponentManager.instance
IssueService issueService = cm.issueService
Long customFieldId = 10000 // Id of your customfield
String[] myValues = ["15345","12345"]  // String array of valid option Ids for your customfield
User myUser  = cm.jiraAuthenticationContext.getLoggedInUser()
IssueInputParameters issueInputParameters

// Find your issues here and use the following code on each of them

issueInputParameters = new IssueInputParametersImpl([:])
issueInputParameters.addCustomFieldValue(customFieldId, myValues)

validationResult = issueService.validateUpdate(myUser, issue.id, issueInputParameters)

if (validationResult.isValid()) {
    updateResult = issueService.update(myUser, validationResult)
    if (!updateResult.isValid()) {
        updateResult.errorCollection.errors.each {log?.error "Error: $it"}
    }
} else {
    validationResult.errorCollection.errors.each {log?.error "Error: $it"}
}

Be aware that the value array will override the current selected checkboxes for the issue.

Henning

Oh... you already got it :-)

Thats working fine, with some parameters...

But only I must switch off mail-sending durring this batch-opperation.

How can I switch off mails for only the affected issues and this shortly time?

To supress notifications you can surround the issueService code with

import com.atlassian.jira.util.ImportUtils

boolean wasNotify = ImportUtils.isEnableNotifications()
try {
	ImportUtils.setEnableNotifications(false)

	// update Issue ..
}
finally {
	ImportUtils.setEnableNotifications(wasNotify)                   // Reset notification state
}

This disables notifications for the whole system, but maybe for this short time period this is ok for you.

Henning

Or you could try adding

validationResult.getAdditionInputs().put("sendBulkNotification", false)

after the validation, but I didn't tried this by myself. Maybe you could report here if this works :-)

That method is only on TransitionValidationResult...

0 votes
Timothy Chin Community Leader Jan 08, 2013

Have you tried to use the id of the option?

no, thats a diea... I test it today in afternoon.

no success...

MutableIssue issue = ComponentAccessor.getIssueManager().getIssueObject("TEST-1")
 
ArrayList<Long> opt=new ArrayList<Long>();
opt.add(10017L); // ID from the CUSTOMFIELDOPTION-Table
opt.add(10019L);
 
 
CustomField f = ComponentAccessor.getCustomFieldManager.getFieldByName("mycheckboxfield")
issue.setCustomFieldValue(f,opt);

doesn't work

and

OptionsManager opM = ComponentAccessor.getComponentOfType(OptionsManager.class);

MutableIssue issue = ComponentAccessor.getIssueManager().getIssueObject("TEST-1")

ArrayList<Option> opt=new ArrayList<Option>();
opt.add(opM.findByOptionId(10017L));
opt.add(opM.findByOptionId(10019L));
 
CustomField f = ComponentAccessor.getCustomFieldManager.getFieldByName("mycheckboxfield")
issue.setCustomFieldValue(f,opt);

doesn't work

Has anyone more ideas?

I has found flowing text in java-doc:

void com.atlassian.jira.issue.MutableIssue.setCustomFieldValue(CustomField customField, Object value)

Sets a custom field value on this Issue Object, but does not write it to the database. This is highly misleading.
To actually set a custom field value, use CustomField.updateIssue(com.atlassian.jira.issue.fields.layout.field.FieldLayoutItem, MutableIssue, java.util.Map)

<dl><dt>Parameters:</dt><dd>customField the CustomField</dd><dd>value the value.</dd></dl>

The value is correctly assign the issue-object (last code-snippet), when I hover in eclipse-debug, but it was not store in database!

How I get the content for variable com.atlassian.jira.issue.fields.layout.field.FieldLayoutItem ??

Why do you want to write a plugin? Just use JIRA CLI and get them updated using a script.

https://bobswift.atlassian.net/wiki/display/JCLI/JIRA+Command+Line+Interface

I donn't have any practical experience with JIRA-CLI.

Can you write a example? I must check before some constulations in description-text and other field before I can fill my custom-value with the dynamic data's. Additional the script must be execute by an external application... So I need a call over HTTP/HTTPs (for example Servlet or Webservices).

I thik thats to comlex for JIRA-CLI... Or wath is you opinion?

http://lmgtfy.com/?q=CLI+sample+for+update+issue+custom+field+JIRA

CLI is the easiest, for doing some thing for one time, no one usually writes a plugin.

If you want to really write some java code, then you can use the Script Runner Plugin and then use the script console.

https://studio.plugins.atlassian.com/wiki/display/GRV/Script+Runner#ScriptRunner-ScriptRunnerpanel

When I wirte java in ScriptRunnerPlugin, I has the same problem that I wrote in topic. Whats the different when I use Eclipse (with code-assistent, svn, ...) and write a plugin or I write java with plain-text-editor in ScriptRunnerPluin?

I need in both a solution to assign my value to the custom-checkbox-field.

Well, that was just my preference, as I would just download CLI, check the examples in Bob's page, run a 'for' loop in my shell to update all the issues (probaly I will also 'getIssueList' using CLI first to get a list of issues).

Check out writing the plugin, it may be easier for you.

Suggest an answer

Log in or Sign up to answer
This widget could not be displayed.
This widget could not be displayed.
Community showcase
Published Thursday in Confluence

Confluence CVEs and common questions

Two vulnerabilities have been published for Confluence Server and Data Center recently: March 20, 2019 CVE-2019-3395 / CVE-2019-3396 April 17, 2019 CVE-2019-3398 The goal of this article is...

82 views 0 9
Read article

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