Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,293,855
Community Members
 
Community Events
165
Community Groups

Set the Organization using Test Field

Cleaning up imported data, where the Orgnization value was imported into a text field.  We now need to clean this up to get more accurate reports etc.

I've gotten so far with extracting the value, but unbale to set the Organization field, using Script Runner.

enableCache = {-> false}

import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.event.issue.AbstractIssueEventListener;
import com.atlassian.jira.event.issue.IssueEvent;
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.util.ImportUtils;
import com.atlassian.jira.issue.index.IssueIndexingService;
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.web.bean.PagerFilter
import org.apache.log4j.Level
import org.apache.log4j.Logger
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder

import com.atlassian.jira.user.*
import com.atlassian.jira.bc.user.search.UserSearchService
import com.atlassian.sal.api.user.UserManager
import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate
import groovy.json.*
import groovy.transform.BaseScript

 

def appUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def linkManager = ComponentAccessor.getIssueLinkManager()
def searchService = ComponentAccessor.getComponent(SearchService.class)
def issueManager = ComponentAccessor.getIssueManager()
def ScriptLog = Logger.getLogger("Jira Log")
def userManager = ComponentAccessor.getUserManager();
def customFieldManager = ComponentAccessor.getCustomFieldManager();
log.setLevel(Level.DEBUG)

def currentUser = userManager.getUserByName("JIRA ADMIN")

// def jqlSearch = "\"Company Code (T)\" is not EMPTY AND \"Company Name (T)\" is not EMPTY ORDER BY cf[10610] ASC"
def jqlSearch = "KEY = ALOGS-1538"

def CompanyName = customFieldManager.getCustomFieldObjectsByName("Company Name (T)")
def OrganizationsField = customFieldManager.getCustomFieldObjectsByName("Organizations")

SearchService.ParseResult parseResult = searchService.parseQuery(appUser, jqlSearch)
if (parseResult.isValid()) {
def searchResult = searchService.search(appUser, parseResult.getQuery(), PagerFilter.getUnlimitedFilter())
def issues = searchResult.results.collect {issueManager.getIssueObject(it.id)}
ScriptLog.info("Issues Found " + issues.size)
issues.each { issue ->
ScriptLog.info("Name: " + issue.getCustomFieldValue(CompanyName))
def mutableIssue = ComponentAccessor.getIssueManager().getIssueObject(issue.getKey());
boolean isIndex = ImportUtils.isIndexIssues();
ImportUtils.setIndexIssues(true);

//OrganizationsField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(OrganizationsField), issue.getCustomFieldValue(CompanyName)), new DefaultIssueChangeHolder())

//issue.setCustomFieldValue(OrganizationsField, issue.getCustomFieldValue(CompanyName))
ComponentAccessor.getIssueManager().updateIssue(currentUser, issue, EventDispatchOption.ISSUE_UPDATED, false)

IssueIndexingService IssueIndexingService = (IssueIndexingService) ComponentAccessor.getComponent(IssueIndexingService.class);
IssueIndexingService.reIndex(mutableIssue);
ImportUtils.setIndexIssues(isIndex);

}
} else {
ScriptLog.error("Invalid JQL :" + jqlSearch)
}
ScriptLog.info("Completed")

 

0 answers

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Apps & Integrations

Marketplace Partner Spotlight: AppLiger

This month the spotlight is on AppLiger. We caught up with Pavel Pavlovsky, CEO and Product Manager, to learn how the company started and what fuels the team's creativity.    Atlassian:...

336 views 0 7
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you