Get value from profields custom field (scriptrunner)

Mauricio Rodriguez
Contributor
March 10, 2021

I am trying to get value from a profields custom field but the code is not working. 

Versión:7.7.1

Proveedor:DEISER

Clave de la aplicación:com.deiser.jira.profields

 

import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import com.deiser.jira.profields.api.field.status.StatusField
import com.deiser.jira.profields.api.field.FieldService
import com.deiser.jira.profields.api.field.Field
import com.deiser.jira.profields.api.value.ValueService

@WithPlugin("com.deiser.jira.profields")



// Project key

pkey = XXXX

def project = ComponentAccessor.getProjectManager().getProjectByCurrentKeyIgnoreCase(pkey)
def fieldService = ComponentAccessor.getOSGiComponentInstanceOfType(FieldService.class)

Field statusField = (StatusField) fieldService.get(25)
def statusValue = profieldsFieldService.getValue(project, (StatusField) statusField)


 

 

The log error is following.

org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'com.deiser.jira.profields.impl.field.type.generalfield.GeneralFieldImpl@6e5379a' with class 'com.deiser.jira.profields.impl.field.type.generalfield.GeneralFieldImpl' to class 'com.deiser.jira.profields.api.field.status.StatusField' at Script2600$_buildHTMLTableOfIssues_closure1$_closure4$_closure5$_closure7$_closure8.doCall(Script2600.groovy:221) at Script2600$_buildHTMLTableOfIssues_closure1$_closure4$_closure5$_closure7$_closure8.doCall(Script2600.groovy) at Script2600$_buildHTMLTableOfIssues_closure1$_closure4$_closure5$_closure7.doCall(Script2600.groovy:152) at Script2600$_buildHTMLTableOfIssues_closure1$_closure4$_closure5.doCall(Script2600.groovy:151) at Script2600$_buildHTMLTableOfIssues_closure1$_closure4$_closure5.doCall(Script2600.groovy) at Script2600$_buildHTMLTableOfIssues_closure1$_closure4.doCall(Script2600.groovy:138) at Script2600$_buildHTMLTableOfIssues_closure1$_closure4.doCall(Script2600.groovy) at Script2600$_buildHTMLTableOfIssues_closure1.doCall(Script2600.groovy:136) at Script2600$_buildHTMLTableOfIssues_closure1.doCall(Script2600.groovy) at Script2600.buildHTMLTableOfIssues(Script2600.groovy:119) at Script2600$buildHTMLTableOfIssues$0.callStatic(Unknown Source) at Script2600.run(Script2600.groovy:71)

1 answer

1 accepted

5 votes
Answer accepted
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.
March 10, 2021

Hi Mauricio, I'm Carlos from DEISER and I'm working in Profields Team.

First of all, are you sure that the field id "25" is the id of a status field? the error you get is probably thrown because of that.

After you check that, you need to change a little bit the script. The value of the field in a project is obtained with the ValueService

The script below should be work:

import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import com.deiser.jira.profields.api.field.status.StatusField
import com.deiser.jira.profields.api.field.FieldService
import com.deiser.jira.profields.api.field.Field
import com.deiser.jira.profields.api.value.ValueService
import com.atlassian.jira.component.ComponentAccessor

@WithPlugin("com.deiser.jira.profields")

// Project key
pkey = "XXX"

// Status field id
fieldId = ??

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

def project = ComponentAccessor.getProjectManager().getProjectByCurrentKeyIgnoreCase(pkey)
def statusField = fieldService.get(fieldId)
def statusValue = valueService.getValue(project, (StatusField) statusField)

If you continue getting errors please, let me know

Regards

Carlos 

Mauricio Rodriguez
Contributor
March 11, 2021

Hi Carlos.  

Thank you for your support. 

I continue having problems. 

 

// get profields project field (text custom field)
 def fieldService = ComponentAccessor.getOSGiComponentInstanceOfType(FieldService.class)
def valueService = ComponentAccessor.getOSGiComponentInstanceOfType(ValueService.class)
cf = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(new Long(11387))

//get project key from jira custom field cascading list 
def pkey = ((Map<String, String>) issueObj.getCustomFieldValue(cf)).get(null)
def fieldId = 25

def project = ComponentAccessor.getProjectManager().getProjectByCurrentKeyIgnoreCase(pkey)
def statusField = fieldService.get(fieldId)
def statusValue = valueService.getValue(project, (StatusField) statusField)

 

Message log error is: 

groovy.lang.MissingMethodException: No signature of method: com.atlassian.jira.project.CachingProjectManager.getProjectByCurrentKeyIgnoreCase() is applicable for argument types: (com.atlassian.jira.issue.customfields.option.LazyLoadedOption) values: [MTRA] Possible solutions: getProjectByCurrentKeyIgnoreCase(java.lang.String)

 

-----

After that message chaged 

 

def project = ComponentAccessor.getProjectManager().getProjectByCurrentKeyIgnoreCase(pkey.toString())

 

Message log error is: 

org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'com.deiser.jira.profields.impl.field.type.generalfield.GeneralFieldImpl@6c42a498' with class 'com.deiser.jira.profields.impl.field.type.generalfield.GeneralFieldImpl' to class 'com.deiser.jira.profields.api.field.status.StatusField'

 

 

Could you help me please? 

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.
March 11, 2021

Do you check if the field with id "25" is a status field?

Mauricio Rodriguez
Contributor
March 11, 2021

Absolutely and I was trying with other id fields. But is not working.  

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.
March 11, 2021

Can you execute this?

import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import com.atlassian.jira.component.ComponentAccessor
import com.deiser.jira.profields.api.field.FieldService

@WithPlugin("com.deiser.jira.profields")

def fieldService = ComponentAccessor.getOSGiComponentInstanceOfType(FieldService.class)

fieldService.get(25).class.name + ": " + fieldService.get(25).type

In order to know what kind of field is returned by the field service, because it seems a problem with the field type

Sorry: I edited the answer, because it has errors

Mauricio Rodriguez
Contributor
March 11, 2021

import com.atlassian.jira.component.ComponentAccessor
import com.deiser.jira.profields.api.field.FieldService
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
@WithPlugin("com.deiser.jira.profields")

def fieldService = ComponentAccessor.getOSGiComponentInstanceOfType(FieldService.class)
fieldService.get(25).class.name

 

Log info: 

com.deiser.jira.profields.impl.field.type.generalfield.GeneralFieldImpl

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.
March 11, 2021

I had edited the script but you got it before, can you execute the edited script again? I fix errors and change the result line

Mauricio Rodriguez
Contributor
March 11, 2021

Is not working this line

pkey is a string value with project Key.   

image.png

 



def project = ComponentAccessor.getProjectManager().getProjectByCurrentKeyIgnoreCase(pkey)



groovy.lang.MissingMethodException: No signature of method: com.atlassian.jira.project.CachingProjectManager.getProjectByCurrentKeyIgnoreCase() is applicable for argument types: (com.atlassian.jira.issue.customfields.option.LazyLoadedOption) values: [CADR] Possible solutions: getProjectByCurrentKeyIgnoreCase(java.lang.String)

 

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.
March 11, 2021

Hi Mauricio

I think you have two different problems:

  • You get a project field object with id 25 that doesn't convert to StatusField
  • You try to get a project by key with a LazyLoadedOption instead of String

In the code pasted below I explain each case

Please configure the two IDs of the fields, for the cascading and for the project status field.


import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import com.deiser.jira.profields.api.field.status.StatusField
import com.deiser.jira.profields.api.field.FieldService
import com.deiser.jira.profields.api.field.Field
import com.deiser.jira.profields.api.value.ValueService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.customfields.option.LazyLoadedOption

@WithPlugin("com.deiser.jira.profields")

// Cascading custom field id
def CASCADING_ID = 10000L

// Status field id
def STATUS_FIELD_ID = 100

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

// Get the cascading custom field
def customField = ComponentAccessor.customFieldManager.getCustomFieldObject(CASCADING_ID)

// Get the value of the cascading field
// This field returns a map that has two keys "null" and "1" and the values are LazyLoadedOptions
// To get the String value of a LazyLoadedOption you need to call the getValue() method of the object
// Then you get the values of each drop down like this:
// valuesMap.get(null).value -> first drop
// valuesMap.get(1).value -> second drop
def projectKey = ((Map<String, LazyLoadedOption>)issueObj.getCustomFieldValue(customField)).get(null).value

// Get the project with the previous key
def project = projectManager.getProjectByCurrentKeyIgnoreCase(projectKey)

// Get the Profields project Status field
def statusField = fieldService.get(STATUS_FIELD_ID)

// Get the value of the status field in the project
def statusValue
try{
statusValue = valueService.getValue(project, (StatusField) statusField)
}catch(def ex){
log.error "Error casting the field: ${ex.message}"
log.error "Field: ${statusField}"
log.error "Class: ${statusField.class.name}"
log.error "Type: ${statusField.type}"
}

// Return the value
return statusValue?statusValue.text:"none"

 

Hope it helps

Regards

Carlos

Mauricio Rodriguez
Contributor
March 12, 2021

Hi Carlos.  

Thanks a lot for your support. 

The code has not errors but is not working. :(.  I changed profields custom field and the result is same. 

Log error is following: 

 

Field: com.deiser.jira.profields.impl.field.type.generalfield.GeneralFieldImpl@457fde63

Class: com.deiser.jira.profields.impl.field.type.generalfield.GeneralFieldImpl 

Type: TEXT

 

2021-03-12 12:42:07,681 ERROR [runner.ScriptBindingsManager]: Error casting the field: Cannot cast object 'com.deiser.jira.profields.impl.field.type.generalfield.GeneralFieldImpl@62f2722e' with class 'com.deiser.jira.profields.impl.field.type.generalfield.GeneralFieldImpl' to class 'com.deiser.jira.profields.api.field.status.StatusField' 2021-03-12 12:42:07,681 ERROR [runner.ScriptBindingsManager]: Field: com.deiser.jira.profields.impl.field.type.generalfield.GeneralFieldImpl@62f2722e 2021-03-12 12:42:07,681 ERROR [runner.ScriptBindingsManager]: Class: com.deiser.jira.profields.impl.field.type.generalfield.GeneralFieldImpl 2021-03-12 12:42:07,681 ERROR [runner.ScriptBindingsManager]: Type: TEXT

 

 

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.
March 14, 2021

Hi Mauricio

The id is related a Text field. Where do you get this id? Can you send a screenshot with the field and the type?

Mauricio Rodriguez
Contributor
March 15, 2021

Thanks Carlos. 

 

I get id profields custom field from profields field section. Selecting each field are showing ID when you put your mouse pointer over profield field. 

 

I tried to get information about other fields but the result is same.  

 

image.png

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.
March 15, 2021

Hi Mauricio

Depends on the field type, you need to cast to its related field type class in the script

fieldTypes.png

 

For example, for the field in the screenshot you should do:

// Get the Profields project Text field
def field = fieldService.get(10)

// As the field is a field of type text, you get the value casting to the class TextField
// you will need the import: import com.deiser.jira.profields.api.field.text.TextField
def value = valueService.getValue(project, (TextField) field)

 

Can you add the screenshot for the field 25 in order to investigate what would be the problem?

Mauricio Rodriguez
Contributor
March 15, 2021

Hi Carlos.

Is working now. Thanks a lot for your support. 

Regards!  

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.
March 15, 2021

Great!

Don't hesitate to contact us if you have any further questions.

Regards

Suggest an answer

Log in or Sign up to answer