Profields script field - time spent for a specific project

Doug Snyder April 16, 2019

I am trying to figure out how to get the value of a project custom field that was created with ProFields.  I have the below script that gets me the project from which I can see the values of standard fields.  I am also able to identify the correct custom field.  How do I get the value for this custom field for the given project?

import com.atlassian.jira.bc.project.ProjectService
import com.atlassian.jira.component.ComponentAccessor

def projectKey = "AIU20180523"

def projectManager = ComponentAccessor.projectManager
def projectService = ComponentAccessor.getComponent(ProjectService)
def existedProject = projectManager.getProjectObjByKey(projectKey)

if (existedProject) {
    log.warn("The project with key ${projectKey} exists")
}

def project = ComponentAccessor.getProjectManager().getProjectByCurrentKey(projectKey);
log.warn ("Project name: " + project.name)
def customField =  ComponentAccessor.getCustomFieldManager().getCustomFieldObject(11000L);
log.warn ("Field name: " + customField)

 

As shown below the log is showing the correct results for everything scripted, I just need the actual value of the Adjusted Completion Date profiled custom field for the project key AIU20180523.

2019-04-16 15:27:06,603 WARN [runner.ScriptRunnerImpl]: The project with key AIU20180523 exists

2019-04-16 15:27:06,603 WARN [runner.ScriptRunnerImpl]: Project name: Avaya IC Upgrage-Exec

2019-04-16 15:27:06,603 WARN [runner.ScriptRunnerImpl]: Field name: Adjusted Completion Date

 

2 answers

1 accepted

3 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.
April 16, 2019

Hi Doug

I'm Carlos from DEISER development team.

Profields' fields are stored at project level. You must have a project field called *Adjusted Completion Date*

If you were mapped the Profields' field into a issue custom field and you are setting a script field of ScriptRunner you can get the value with:

issue.getCustomFieldValue(CustomField customField)

(https://docs.atlassian.com/software/jira/docs/api/7.1.2/com/atlassian/jira/issue/Issue.html#getCustomFieldValue-com.atlassian.jira.issue.fields.CustomField-)

 

Your code will be:

import com.atlassian.jira.bc.project.ProjectService
import com.atlassian.jira.component.ComponentAccessor

def projectKey = "AIU20180523"

def projectManager = ComponentAccessor.projectManager
def projectService = ComponentAccessor.getComponent(ProjectService)
def existedProject = projectManager.getProjectObjByKey(projectKey)

if (existedProject) {
log.warn("The project with key ${projectKey} exists")
}

def project = ComponentAccessor.getProjectManager().getProjectByCurrentKey(projectKey);
log.warn ("Project name: " + project.name)
def customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(11000L);
log.warn ("Field name: " + customField)

def value = issue.getCustomFieldValue(customField)

 

you can also get the value directly from the Profields' field, if you have problems with previous code don't hesitate to ask me.


Regards.

Carlos

Doug Snyder April 17, 2019

 

Hi Carlos,

Thank you for the reply.  I am new to all of this, so I am not quite following along.  I am using this Profields field at the project level, so I do not actually have an issue to look up.  As a result, when adding "def value" as indicated, I receive the following:

IssueCapture.PNG

You mention that I can get the value directly from the Profields field.  What would the code be for doing this for the specific project for which I have established context based on the given projectKey?

Doug Snyder April 17, 2019

I have figured out what I need to do.  In case anyone else comes across this post, here is the code that allowed me to get the value:

import com.atlassian.jira.bc.project.ProjectService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import com.deiser.jira.profields.api.field.FieldService
import com.deiser.jira.profields.api.value.ValueService
import com.deiser.jira.profields.api.field.date.DateField

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

def projectKey = "AIU20180523"

def valueService = ComponentAccessor.getOSGiComponentInstanceOfType(ValueService.class)
def fieldService = ComponentAccessor.getOSGiComponentInstanceOfType(FieldService.class)
def projectManager = ComponentAccessor.projectManager
def projectService = ComponentAccessor.getComponent(ProjectService)
def existedProject = projectManager.getProjectObjByKey(projectKey)

if (existedProject) {
    log.warn("The project with key ${projectKey} exists")
}

def project = ComponentAccessor.getProjectManager().getProjectByCurrentKey(projectKey);
log.warn ("Project name: " + project.name)
def dField = fieldService.get(27)  // 27 is the Profields ID for the Adjusted Completion Date
log.warn ("Field name: " + dField)
def dAdjustedCompletionDate = valueService.getValue(project,(DateField) dField)

Like SWAPNIL SRIVASTAV likes this
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.
April 24, 2019

Hi Doug.

Sorry I didn't see those replies. I think you were in a issue script field context and it seems that you were in a console script or similar and because of that you didn't have the issue object.

I'm really glad that you made it work.

Regards, Carlos

SWAPNIL SRIVASTAV May 27, 2020

Hello @Doug Snyder ,

I have tried this script but it gives a Static type check error at the last line "Cannot find matching method". Neither the last line works not the second last (the one which is commented). Kindly help

import com.atlassian.jira.component.ComponentAccessor
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.onresolve.scriptrunner.runner.customisers.WithPlugin

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

def projectKey = "XXXX"

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


def project = ComponentAccessor.getProjectManager().getProjectByCurrentKey(projectKey);
log.error "Project name: " + project.name

def CustomerFieldID = 1;
Field appManager = fieldService.get(CustomerFieldID);
//def appManagerValue = valueService.getValue(project, appManager).toString();
def appManagerValue = appManager.getValueInProject(project)

0 votes
Doug Snyder May 27, 2020

@SWAPNIL_SRIVASTAV,

In the line 

//def appManagerValue = valueService.getValue(project, appManager).toString();

I am not sure you are making the call correctly.  From your code snippet I cannot tell the type of your CustomerFieldID, but that needs to be included in the call to the getValue method.  However, note that in the call I make in my code, I have identified that the Profields field is a DateField in the call:

def dAdjustedCompletionDate = valueService.getValue(project,(DateField) dField)

and I have included an import for Profield date fields:

import com.deiser.jira.profields.api.field.date.DateField

You may want to review the API documentation to see the proper way to make the call to getValue based on your data type.

SWAPNIL SRIVASTAV May 29, 2020

Hello @Doug Snyder ,

Thank you for the suggestions.

It worked. I changed the line to:

def appManagerValue = valueService.getValue(project, (TextField) appManager).toString()

and added:

import com.deiser.jira.profields.api.field.text.TextField

Best Regards

Swapnil Srivastav

Suggest an answer

Log in or Sign up to answer