Missed Team ’24? Catch up on announcements here.

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

Script to clear/reset a Custom Field (Date Time) properly

johann toom
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
May 7, 2014

I am trying to clear a Custom Field of type Date Time with IssueService by sending "null" as a parameter, it resets the Field but not reindexing the Issue (it's still showing in Filter), here is my script for Script Runner 2.1.17, Jira 6.2.4 (IssueService not getting through to storeModifiedFields(...) and therefore to issueEventManager.dispatchEvent(...) because of empty modifiedFields):

import com.atlassian.jira.issue.util.DefaultIssueChangeHolder;
import com.atlassian.jira.bc.issue.search.SearchService;
import com.atlassian.jira.bc.issue.search.SearchService.ParseResult;
import com.atlassian.jira.bc.issue.IssueService;
import com.atlassian.jira.bc.issue.IssueService.IssueResult;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.security.JiraAuthenticationContext;
import com.atlassian.jira.issue.search.SearchResults;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.ModifiedValue;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.fields.layout.field.FieldLayoutManager;
import com.atlassian.jira.issue.fields.layout.field.FieldLayoutItem;
import com.atlassian.jira.issue.IssueInputParameters;
import com.atlassian.jira.web.bean.PagerFilter;

filterName = "10001";
String jqlSearch = "filter= \"" + filterName + "\"";

JiraAuthenticationContext authenticationContext = ComponentAccessor.getJiraAuthenticationContext();
SearchService searchService = ComponentAccessor.getComponentOfType(SearchService .class);
loggedInUser = authenticationContext.getLoggedInUser();
ParseResult parseResult = searchService.parseQuery(loggedInUser, jqlSearch);

textResult = "not clear";

if (parseResult.isValid()) {
	SearchResults results = searchService.search(loggedInUser, parseResult.getQuery(), PagerFilter.getUnlimitedFilter());
	final List issues = results.getIssues();

	for (Issue issue in issues) {
		CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
		customField = customFieldManager.getCustomFieldObject("customfield_10000");

		// sending null as new value
		modifiedValue = new ModifiedValue(issue.getCustomFieldValue(customField), null);
		
		FieldLayoutManager fieldLayoutManager = ComponentAccessor.getFieldLayoutManager();
		FieldLayoutItem fieldLayoutItem = fieldLayoutManager.getFieldLayout(issue).getFieldLayoutItem(customField);
		changeHolder = new DefaultIssueChangeHolder();

		customField.updateValue(fieldLayoutItem, issue, modifiedValue, changeHolder);
		
		IssueService issueService = ComponentAccessor.getIssueService();
		IssueInputParameters issueInputParameters = issueService.newIssueInputParameters();

		// sending null again
		issueInputParameters.addCustomFieldValue(customField.getIdAsLong(), null);

		validationResult = issueService.validateUpdate(loggedInUser, issue.getId(), issueInputParameters);
		if (validationResult.isValid()) {
			IssueResult result = issueService.update(loggedInUser, validationResult);
			if (result.isValid()) {
				textResult = "clear: " + issue.getCustomFieldValue(customField);
			}
		}		
	}
	return textResult;
}

So what's the proper way to reset/clear Date Time Custom Field's value?

1 answer

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

0 votes
Answer accepted
johann toom
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
May 7, 2014

ok got it... lines 35 - 42 are dispensable and there were no event sent since i was changing the value twice (from "12/12/2012 12:12 AM" to null and then again to null), also a problem was in line 48: customField.getIdAsLong() instead of .getId()

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.
May 8, 2014

Problem solved then? If so you can mark your answer as correct, save other people fretting over unanswered questions.

TAGS
AUG Leaders

Atlassian Community Events