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

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

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

🍻🍂Apptoberfest Update: Upcoming Virtual Events 🎉

Hello Community! I hope you've been enjoying the 🍂Apptoberfestivities🍂 (I know I have!) The event is heating up next week with a series of virtual events that we're calling the 🍻🍂Partner App ...

319 views 3 14
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