Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

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

Marc January 8, 2013

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

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

2 votes
Answer accepted
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.
January 9, 2013

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

Marc January 9, 2013

Can you wirte a smal example-code, please?

Renjith Pillai
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.
January 9, 2013

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

Marc January 9, 2013
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?

Marc January 9, 2013

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

work fine!

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.
January 9, 2013

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

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.
January 9, 2013

Oh... you already got it :-)

Marc January 9, 2013

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?

2 votes
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.
January 9, 2013

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

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.
January 9, 2013

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 :-)

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.
January 9, 2013

That method is only on TransitionValidationResult...

0 votes
Renjith Pillai
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.
January 9, 2013

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

Marc January 9, 2013

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?

Renjith Pillai
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.
January 9, 2013

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

Marc January 9, 2013

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.

Renjith Pillai
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.
January 9, 2013

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.

0 votes
Timothy
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.
January 8, 2013

Have you tried to use the id of the option?

Marc January 9, 2013

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

Marc January 9, 2013

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?

Marc January 9, 2013

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 ??

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

TAGS
AUG Leaders

Atlassian Community Events