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,298,096
Community Members
 
Community Events
165
Community Groups

Profields post_function - access jira fields from Profields plugin

Hello team

 

My code bellow work perfectly in script field , but when i move it to a profields post function an error appear . I need help please.

Error = startup failed: Script1.groovy: 13: unable to resolve class com.atlassian.jira.jql.parser.JqlQueryParser @ line 13, column 1. import com.atlassian.jira.jql.parser.JqlQueryParser ^  

My code :

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.project.Project
import com.deiser.jira.profields.api.field.FieldService
import com.deiser.jira.profields.api.field.item.status.StatusItemService
import com.deiser.jira.profields.api.field.status.StatusField
import com.deiser.jira.profields.api.value.ValueService
import com.atlassian.jira.issue.search.SearchProvider


import com.atlassian.jira.jql.parser.JqlQueryParser
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.index.IssueIndexingService
import com.atlassian.jira.util.ImportUtils
import com.atlassian.jira.issue.Issue

 


def sdUser = ComponentAccessor.getJiraAuthenticationContext().getUser() // we can use it if we don't know the name of the user
log.warn("the user key is " + sdUser)
def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)
def searchProvider = ComponentAccessor.getComponent(SearchProvider)
def query1 = jqlQueryParser.parseQuery("project = KP")
def query2 = jqlQueryParser.parseQuery("project = KP and status =\"Done\" ")
def results1 = searchProvider.search(query1, sdUser, PagerFilter.getUnlimitedFilter())
def results2 = searchProvider.search(query2, sdUser, PagerFilter.getUnlimitedFilter())

def valueService = ComponentAccessor.getOSGiComponentInstanceOfType(ValueService.class)
def fieldService = ComponentAccessor.getOSGiComponentInstanceOfType(FieldService.class)
def statusItemService = ComponentAccessor.getOSGiComponentInstanceOfType(StatusItemService.class)


Project project = issue.projectObject

def statusField = (StatusField) fieldService.get(9)


def newStatusItem = statusItemService.getItemByText((StatusField) statusField, "CLÔTURÉ")

valueService.setValue(project, (StatusField) statusField, newStatusItem)

 

 

 

2 answers

Hi @Karim Blehadj

The class JqlQueryParser is not accesible from Profields. We are working to include it in the app.

You can do the same with the following script. I discard the end of the script where you are setting the status item. As you can see, you should use the status id instead of the text to search by it. You can find this id in the status administration inside de text link of the "Edit" link.

 

import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.jql.builder.JqlQueryBuilder
import com.atlassian.jira.web.bean.PagerFilter

def searchService = ComponentAccessor.getOSGiComponentInstanceOfType(SearchService.class)

// we can use it if we don't know the name of the user
def loggedInUser = ComponentAccessor.jiraAuthenticationContext.getLoggedInUser()

def jqlQueryBuilder = JqlQueryBuilder.newBuilder()
def clause = jqlQueryBuilder.newClauseBuilder()
.project("KP")
.and()
.status("10000") // You should use the status id
.buildClause()
def query = jqlQueryBuilder.where().addClause(clause).buildQuery()

def issues = searchService.search(loggedInUser, query, PagerFilter.unlimitedFilter).issues

hi @carlos_fernandez 

I changed my code without the JQL . But there are a problem , the last line doesn't work , the rest it's okay , and it seems weird. Please Help .

 

My code is below:

 

 

import com.atlassian.jira.component.ComponentAccessor

import com.atlassian.jira.project.Project
import com.deiser.jira.profields.api.field.number.NumberField
import com.deiser.jira.profields.api.field.FieldService
import com.deiser.jira.profields.api.field.item.status.StatusItemService
import com.deiser.jira.profields.api.field.status.StatusField
import com.deiser.jira.profields.api.value.ValueService
import com.atlassian.jira.issue.Issue

def valueService = ComponentAccessor.getOSGiComponentInstanceOfType(ValueService.class)
def fieldService = ComponentAccessor.getOSGiComponentInstanceOfType(FieldService.class)

def statusItemService = ComponentAccessor.getOSGiComponentInstanceOfType(StatusItemService.class) //for the status item
Project project = issue.projectObject

// get value of done and created issue
def donefield = (NumberField)fieldService.get(41)// ID of field number done
def createdfield = (NumberField)fieldService.get(42)// ID of field number created

def numbervaluecreated = valueService.getValue(issue.projectObject,(NumberField) createdfield)
def numbervaluedone = valueService.getValue(issue.projectObject,(NumberField) donefield)

// set value of done field
valueService.setValue(project,(NumberField)donefield,numbervaluedone+ 1.0)
log.warn("the value of created issues after updating is" + numbervaluedone + 1.0)

 

def statusField = (StatusField) fieldService.get(9)

// Gets the status item base on a text
def newStatusItem = statusItemService.getItemByText((StatusField) statusField, "CLÔTURÉ")

// Set the value in the "other" project

//(this line doesn't work)
valueService.setValue(project, (StatusField) statusField, newStatusItem)

Hi @Karim Belhadj

Can you add these next lines before the line that is not working and send the related errors of the atlassian-jira.log at the moment of the execution?

 

log.warn("Project: ${project}")
log.warn("Field: ${statusField}")
log.warn("New item: ${newStatusItem}")

Hi @Karim Belhadj

The class JqlQueryParser is not accesible from Profields. We are working to include it in the app.

You can do the same with the following script. I discard the end of the script where you are setting the status item. As you can see, you should use the status id instead of the text to search by it. You can find this is in the status administration inside de text link of the "Edit" link.

 

import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.jql.builder.JqlQueryBuilder
import com.atlassian.jira.web.bean.PagerFilter

def searchService = ComponentAccessor.getOSGiComponentInstanceOfType(SearchService.class)

// we can use it if we don't know the name of the user
def loggedInUser = ComponentAccessor.jiraAuthenticationContext.getLoggedInUser()

def jqlQueryBuilder = JqlQueryBuilder.newBuilder()
def clause = jqlQueryBuilder.newClauseBuilder()
.project("KP")
.and()
.status("10000") // You should use the status id
.buildClause()
def query = jqlQueryBuilder.where().addClause(clause).buildQuery()

def issues = searchService.search(loggedInUser, query, PagerFilter.unlimitedFilter).issues 

Hi @carlos_fernandez ,

 

Any timeline on when the JQLQueryParser will be accessible in ProFields?

 

I´m currently trying to run a rather complicated query using Altavista´s ScriptRunner "issueFunction in linkedIssuesOf("xxx")", and I´m not familiar with how to build those queries that include custom fields using the JQLQueryBuilder.

Suggest an answer

Log in or Sign up to answer
TAGS

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