Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Scriptrunner: check if a customfield is hidden or not

Jihad Sabra June 2, 2022

For a given issue type, I have behaviours that show/hide certain custom fields on the edit screen based on certain conditions. These custom fields are not marked as "Required" .  On the resolve issue screen, I am adding a workflow validator to make certain fields mandatory based on the resolution that's being picked. A subset of custom fields will get checked, if they're not hidden and have null values, I'll throw an exception. Is there a way to find out if a certain custom field is visible or not. Sample code below for illustration:

 

import com.opensymphony.workflow.InvalidInputException
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.component.ComponentAccessor

//Don't bother with duplicate resolution
if (issue.getResolution().getName()=="Duplicate") {
  return
}
InvalidInputException errEx = null
List<String> myCFs = ["CF-name1","CF-name2",...."CF-nameN"]

for (int i = 0; i < N; i++) {
def cf = ComponentAccessor.customFieldManager.getCustomFieldObjects(issue).findByName(myCFs[i])
IF ( ***CHECK IF CF IS HIDDEN OR NOT***) {
   String cfVal = issue.getCustomFieldValue(cf)
if (cfVal == null || cfVal.isEmpty()) {
      if (errEx == null) {
         errEx = new InvalidInputException (myCFs[i] + " must have a value")
      } else
          errEx.addError(myCFs[i] + " must have a value")
      }
  }
}
}
}
if (errEx == null) {
return
}
throw errEx

1 answer

1 accepted

Suggest an answer

Log in or Sign up to answer
1 vote
Answer accepted
Andrea Pannitti
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.
June 30, 2022

Hi @Jihad Sabra,

you could call the not documented Jira's API:

/rest/whereismycf/1.0/fields/{id}     [GET]

Query parameters:

Either 'issueKey', or 'projectKey' or 'projectId' parameter must be provided in the request

Also the issueOperation:

0 create
1 edit
2 view

Then you should get the response status that could be "green" or "red". In the first case the field is visible, else it's hidden.

You could test the rest also in web browser calling an url as the following:

http://localhost:8080/rest/whereismycf/1.0/fields/customfield_xxxxx?issueKey=YOUR_ISSUEKEY&issueOperation=1

Jihad Sabra July 1, 2022

Thanks Andrea for the information, that's pretty neat.

TAGS
AUG Leaders

Atlassian Community Events