Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Showing field based on multiselect component label

Parthiban
January 3, 2018

Hi Team,

I want to show custom field based on Component selection. I have written code in Script Runner Behaviour , my code working perfectly when users select one component label. But its not behaving correctly when users select two or more component labels.

 

If users select two or more component labels , I have to show two or more custom fields. 

Note : I have created 40 custom fields and 40 component labels for my client. When ever users select component I need to show/display respective custom fields.

My code is working when users select one component label.

@BaseScript FieldBehaviours fieldBehaviours
def log = Logger.getLogger("com.acme.CreateSubtask");
log.setLevel(Level.DEBUG);

def dashboardCost = getFieldById("customfield_12735");
def devCost = getFieldById("customfield_12736");
def sccdCost = getFieldById("customfield_12737");
def jiraCost = getFieldById("customfield_12738");

def components =getFieldById("components");
def comp= components.getValue()*.getName();

if (comp.toString().contains('Dashboard')){
dashboardCost.setHidden(false);
}
else if(comp.toString().contains('dev')){
devCost.setHidden(false);
}
else if (comp.toString().contains('SCCD')){
sccdCost.setHidden(false);
}
else if (comp.toString().contains('JIRA')){
jiraCost.setHidden(false);
}

2 answers

1 accepted

Suggest an answer

Log in or Sign up to answer
0 votes
Answer accepted
Adrien Low _ServiceRocket_
Community Champion
March 25, 2022

Hello @Shah Baloch ,

Depending on your requirements, it can be Behaviors, post-functions, listeners or Script Fields.

Below is an example using Script Fields with a Number Field template:-

import com.atlassian.jira.component.ComponentAccessor

def customFieldManager = ComponentAccessor.getCustomFieldManager()

def createdDateValue = issue.getCreated()
def issueReportedDate = customFieldManager.getCustomFieldObject("customfield_10800")

def issueReportedDateValue = issue.getCustomFieldValue(issueReportedDate)

if (issueReportedDateValue) {
def daysDiff = createdDateValue - issueReportedDateValue

return daysDiff
}

The output is as below:-

ss1.png

Hope this helps.

Regards.

Shah Baloch
Contributor
March 25, 2022

@Adrien Low _ServiceRocket_thank you for your response. Looks like something is missing, it's showing an error. How can I fix it? Thank you for your help

ScriptRunner_03252022.jpg

Shah Baloch
Contributor
March 25, 2022

@Adrien Low _ServiceRocket_ Sorry to bother you.

The current script is about the Reported date and created date. Is there a way to include the resolution date? I mean if the issue is open it should work like this as you this script. Like if the issue is not resolved it should calculate the date between the Reported date and created date but if the issue gets closed it should calculate the value between "Reported Date" and Resolved Date". Is that possible with a single script and field?

Here is the use case

If resolution = unresolved
Reported Date - Created Date

If resolution = resolved
Reported date - Resolved Date

Thank you for your help.

Shah Baloch
Contributor
March 28, 2022

Hi Adrien, I was able to make it work. Whenever an issue gets created and it'll calculate the reported date and created date, once the issue gets resolved then it changes the value by calculating the reported date and resolved date.

However, I get an error if the reported field is empty. here is the error

java.lang.NullPointerException at org.apache.groovy.dateutil.extensions.DateUtilExtensions.minus(DateUtilExtensions.java:504) at Script163.run(Script163.groovy:19)

This is what I can see from the log:

2022-03-28 14:31:32,691 ERROR [customfield.GroovyCustomField]: ************************************************************************************* 2022-03-28 14:31:32,691 ERROR [customfield.GroovyCustomField]: Script field failed on issue: TP-36, field: Unused java.lang.NullPointerException at org.apache.groovy.dateutil.extensions.DateUtilExtensions.minus(DateUtilExtensions.java:504) at Script163.run(Script163.groovy:19)

Any idea how can I make it work with an empty Reported date? Here is the code that works if the reported date field has a value.

import com.atlassian.jira.component.ComponentAccessor

def customFieldManager = ComponentAccessor.getCustomFieldManager()

def createdDateValue = issue.getCreated()
def resolutionDate = issue.getResolutionDate()

def issueReportedDate = customFieldManager.getCustomFieldObject("customfield_xxxxx")

def issueReportedDateValue = issue.getCustomFieldValue(issueReportedDate)

if (null !=resolutionDate) {

def daysDiff = resolutionDate - issueReportedDateValue

return daysDiff
}
else {
def daysDiff = createdDateValue - issueReportedDateValue
return daysDiff
}

Thank you

Shah Baloch
Contributor
March 29, 2022

Hi @Adrien Low _ServiceRocket_ just wanted to update you that I was able to make it work with an empty date in the reported field. I added a while loop. I don't know it's the best practice but it is working for me. You're welcome to add or remove anything from the code if it's not needed.

Here is the working code:

import com.atlassian.jira.component.ComponentAccessor

def customFieldManager = ComponentAccessor.getCustomFieldManager()

def createdDateValue = issue.getCreated()
def resolutionDate = issue.getResolutionDate()

def issueReportedDate = customFieldManager.getCustomFieldObject("customfield_xxxxx")

def issueReportedDateValue = issue.getCustomFieldValue(issueReportedDate)

while (true)
if (issueReportedDateValue == null) {
break;
}

else {

if (null !=resolutionDate) {

def daysDiff = resolutionDate - issueReportedDateValue

return daysDiff
}
else {
def daysDiff = createdDateValue - issueReportedDateValue
return daysDiff
} }

 

However, I'm still unable to get rid of the "Static type checking" error mentioned in the first screenshot.

I really appreciate your help, without your initial code, I couldn't be able to make it work. Thank you so much!

Adrien Low _ServiceRocket_
Community Champion
March 30, 2022

Hello @Shah Baloch ,

Glad you managed to work out your issues.

Perhaps you can try the below code instead of using a while loop:-

import com.atlassian.jira.component.ComponentAccessor

def customFieldManager = ComponentAccessor.getCustomFieldManager()

def createdDateValue = issue.getCreated()
def resolutionDate = issue.getResolutionDate()

def issueReportedDate = customFieldManager.getCustomFieldObject("customfield_xxxxx")

def issueReportedDateValue = issue.getCustomFieldValue(issueReportedDate)


if (issueReportedDateValue != null) {

    if (resolutionDate != null) {

        def daysDiff = resolutionDate - issueReportedDateValue

        return daysDiff
    }
    else {
        def daysDiff = createdDateValue - issueReportedDateValue
        return daysDiff
    }

}

return null

As for the static type error, you should be able to ignore it as highlighted in the KB below:-

Regards.

Like Shah Baloch likes this
Shah Baloch
Contributor
March 30, 2022

Thank you @Adrien Low _ServiceRocket_ I showed this Issue age field to our team they liked it and they think it's very useful. For another project, we would like to calculate the issue-created date and current date. I'm unable to define the current date. How can I define the current date that I can use to do calculations between the issue created date and the current date?  Thank you for your help.

Shah Baloch
Contributor
March 31, 2022

Hi @Adrien Low _ServiceRocket_ I was able to define the current date

def currentDate = new Date()

One last question, hopefully, it'll be the last one related to this. How can add multiple scripts in a single field? We would like to use this issue age field in multiple projects but all projects do not have a reported date field. For example, in this project we use the Reported date field, the issue created date and resolved date. However, for another project, we would like to use issue created date-current date and created date - resolved date. I just wanted to check if there is a way to use the same field with a different script instead of creating a new field.

Thank you for your help.

0 votes
SHANAVASH MOHAMED
Contributor
May 20, 2025

Hello Team, 

Depending on your requirements achieve through "Script Listener"

 

import com.atlassian.jira.component.ComponentAccessor

def customFieldManager = ComponentAccessor.getCustomFieldManager()

def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

def createdDate = issue.getCreated()

def updatedDate = issue.getUpdated()

if (createdDate && updatedDate) {

    def millisInDay = 1000 * 60 * 60 * 24

    def daysDiff = Math.round((updatedDate.time - createdDate.time) / millisInDay)

    def customField = customFieldManager.getCustomFieldObject("customfield_xxxxxxxx")

    if (customField) {

        // Cast to Double to match custom field type

        issue.setCustomFieldValue(customField, daysDiff.toDouble())

        // Save the updated value

        ComponentAccessor.getIssueManager().updateIssue(user, issue, com.atlassian.jira.event.type.EventDispatchOption.DO_NOT_DISPATCH, false)

    }

}
TAGS
AUG Leaders

Atlassian Community Events