Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Deleted user
0 / 0 points
Next:
badges earned

Your Points Tracker
Challenges
Leaderboard
  • Global
  • Feed

Badge for your thoughts?

You're enrolled in our new beta rewards program. Join our group to get the inside scoop and share your feedback.

Join group
Recognition
Give the gift of kudos
You have 0 kudos available to give
Who do you want to recognize?
Why do you want to recognize them?
Kudos
Great job appreciating your peers!
Check back soon to give more kudos.

Past Kudos Given
No kudos given
You haven't given any kudos yet. Share the love above and you'll see it here.

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

Cant set single select customfield

The task is - in post function find an issue and then change customfiled to other option like in issue where postfunction is.

 

My code is

import org.apache.log4j.Level
import org.apache.log4j.Logger

import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.search.SearchResults
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder

// set up logging
def log = Logger.getLogger("com.example.jira.script.ProcessJQL");

//def Grade = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Grade")
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def Grade = customFieldManager.getCustomFieldObject(14209) //Grade CF
def GradeValue = issue.getCustomFieldValue(cf) //Object option of Grade from issue

log.setLevel(Level.INFO);

// define JQL query
String jqlSearch = "project = INFO AND issueType = Task AND Employee = akim";

// required objects
SearchService searchService = ComponentAccessor.getComponent(SearchService.class);
ApplicationUser user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();
IssueManager issueManager = ComponentAccessor.getIssueManager();

// validate the query
SearchService.ParseResult parseResult = searchService.parseQuery(user, jqlSearch);
if (parseResult.isValid()) {
// do the search
SearchResults searchResult = searchService.search(user, parseResult.getQuery(), PagerFilter.getUnlimitedFilter());

for (Issue issue : searchResult.getResults()) {
log.info("Found issue: " + issue.getKey());

// to update issue convert it to MutableIssue
MutableIssue mutableIssue = issueManager.getIssueObject(issue.getId());
mutableIssue.setSummary(issue.getSummary() + " UPDATED");

def availableOptions = ComponentAccessor.optionsManager.getOptions(Grade.getRelevantConfig(issue))
def optionToSet = availableOptions.find { it.value == newValue }

Grade.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(Grade), optionToSet), new DefaultIssueChangeHolder())

issueManager.updateIssue(user, mutableIssue, EventDispatchOption.ISSUE_UPDATED, false); // false = do not send e-mail notifications
}
} else
log.error("Invalid JQL: " + jqlSearch);

 

Got error:

2021-05-26 18:19:46,088 ERROR [workflow.AbstractScriptWorkflowFunction]: Workflow script has failed on issue CIW-613 for user 'XXXXXXX'. View here: https://XXXXXXXXX/secure/admin/workflows/ViewWorkflowTransition.jspa?workflowMode=live&workflowName=CIW%3A+Project+Management+Workflow&descriptorTab=postfunctions&workflowTransition=91&highlight=5 groovy.lang.MissingPropertyException: No such property: customFieldManager for class: Script3863 at Script3863.run(Script3863.groovy:17)

2 answers

1 accepted

2 votes
Answer accepted

I found the solution here is the code!

 

import org.apache.log4j.Level
import org.apache.log4j.Logger

import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.search.SearchResults
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder

// set up logging
def log = Logger.getLogger("com.example.jira.script.ProcessJQL");

//def Grade = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Grade")
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def Grade = customFieldManager.getCustomFieldObject(14209) //Grade CF
def GradeValue = issue.getCustomFieldValue(Grade) //Object option of Grade from issue

log.setLevel(Level.INFO);

// define JQL query
String jqlSearch = "project = INFO AND issueType = Task AND Employee = akim";

// required objects
SearchService searchService = ComponentAccessor.getComponent(SearchService.class);
ApplicationUser user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();
IssueManager issueManager = ComponentAccessor.getIssueManager();

// validate the query
SearchService.ParseResult parseResult = searchService.parseQuery(user, jqlSearch);
if (parseResult.isValid()) {
// do the search
SearchResults searchResult = searchService.search(user, parseResult.getQuery(), PagerFilter.getUnlimitedFilter());

for (Issue issue : searchResult.getResults()) {
log.info("Found issue: " + issue.getKey());

// to update issue convert it to MutableIssue
MutableIssue mutableIssue = issueManager.getIssueObject(issue.getId());
mutableIssue.setSummary(issue.getSummary() + " UPDATED");
mutableIssue.setCustomFieldValue(Grade, GradeValue)

 

issueManager.updateIssue(user, mutableIssue, EventDispatchOption.ISSUE_UPDATED, false); // false = do not send e-mail notifications
}
} else
log.error("Invalid JQL: " + jqlSearch);

 

 

as you can see i just used    mutableIssue.setCustomFieldValue(Grade, GradeValue)

0 votes
John Funk Community Leader May 26, 2021

Hi Ilya - WOW, that's a lot of code to get a field updated. What if you used a plug in like Automation for Jira? There is a Free Lite version and a paid Pro version that will probably do what you need to do without a lot of code. 

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
TAGS
Community showcase
Published in Confluence

Introducing External Collaboration for Confluence

We’re excited to introduce external collaboration for Confluence, now available in early access. It is available to preview for Confluence Cloud Premium and Enterprise customers. (If you're not on ...

223 views 0 8
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