The Atlassian Community Forums are currently in read-only mode. We will be relaunching on a new platform on September 22 (read more here). We apologize for the extended downtime. For concerns or questions, please email communitymanagers@atlassian.com. See you on the other side, on the new Atlassian Community Forums! :)

×

Forums

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

Hide Create Subtask UI element based on a custom field value condition

VVC
Contributor
October 22, 2024

Trying to hide the "Create Subtask" UI element based on a Single Select Custom Field value (CMR Type equals Normal").  I have tried using Script Runner UI Fragments > Hide system or plugin UI element but it is not working as expected.

Am I missing something in this script? The "Create Sub-task" disappeared completely irrespective of any value in CMR Type field.

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
def issue = ComponentAccessor.getIssueManager().getIssueObject(issue.id)
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def customField = customFieldManager.getCustomFieldObjectByName("CMR Type")
if (customField) {
    def customFieldValue = issue.getCustomFieldValue(customField)
    log.debug("Custom Field Value: ${customFieldValue}")
    return customFieldValue == 'Normal'
} else {
    log.warn("Custom field not found: Your Custom Field Name")
    return false
}

2 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

0 votes
Answer accepted
Radek Dostál
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 Champions.
October 23, 2024

What is the custom field type?

Just checking because == would only work for text fields and their derivates, but not like labels or options and stuff like that.

VVC
Contributor
October 23, 2024

Hi @Radek Dostál - The custom field "CMR Type" type is a single select option. No wonder why this is not working, due to this limitation?

Radek Dostál
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 Champions.
October 24, 2024

Due to what limitation? You're checking reference equality between a LazyLoadedOption class and a string.

 

return "Normal" == customFieldValue?.getValue()

return "Normal".equals(customFieldValue?.getValue())

 

Either one is fine, you just need to compare string with string.

Like VVC likes this
VVC
Contributor
October 24, 2024

Thanks @Radek Dostál . It is working now.

0 votes
Manoj Kumar Gangwar
Community Champion
October 22, 2024

Hi @VVC Please try the script below.

Changes:

  1. Logic Inversion: Changed the condition to check if customFieldValue is not equal to 'Normal'.
  2. Default Return: Adjusted the return value when the custom field is not found to ensure the UI element remains visible in that case.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue

def issue = ComponentAccessor.getIssueManager().getIssueObject(issue.id)
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def customField = customFieldManager.getCustomFieldObjectByName("CMR Type")

if (customField) {
def customFieldValue = issue.getCustomFieldValue(customField)
log.debug("Custom Field Value: ${customFieldValue}")
return customFieldValue != 'Normal' // Return true to show the UI element when it's NOT 'Normal'
} else {
log.warn("Custom field not found: Your Custom Field Name")
return true // Default to showing the UI element if the custom field is not found
}

VVC
Contributor
October 23, 2024

Hi @Manoj Kumar Gangwar - Create Subtask menu item option wouldn't display even after changing the "CMR Type" custom field to any value.

DEPLOYMENT TYPE
SERVER
TAGS
AUG Leaders

Atlassian Community Events