Profields post_function - access jira fields from Profields plugin

Karim Belhadj December 16, 2018

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

0 votes
carlos.fernandez
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.
December 16, 2018

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
Karim Belhadj December 17, 2018

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)

carlos.fernandez
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.
December 18, 2018

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}")
0 votes
carlos.fernandez
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.
December 16, 2018

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 
Efren Fernandez Abeledo May 5, 2022

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.

Leo Diaz _ DEISER
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 2, 2022

Hi @Efren Fernandez Abeledo !

In order to give you a better support, please contact us through our support portal:

https://jira.deiser.com/plugins/servlet/desk/portal/1

Sorry for not getting back to you sooner, but being an open site, we may miss some questions like this.

Hope it helps you!

Leo

Suggest an answer

Log in or Sign up to answer