Forums

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

Disable Scripted fields (Scriptrunner)

Howard Nedd
Contributor
April 30, 2021

I would like to disable a scripted field instead of deleting it. Is the only way to disable it by deleting the entire script, or getting rid of it in screens /issuetypes etc? Seems a bit cumbersome. 

3 answers

2 accepted

3 votes
Answer accepted
Martin Bayer _MoroSystems_ s_r_o__
Community Champion
April 30, 2021

Hi @Howard Nedd , did you try to return null on the first row of your script? It could look like

return null

HERE IS YOUR CODE

The script will be always executed so it can have performance impacts.

If you do not want the script to be executed you will need to remove the field from all the Screens -> set them back when you need it.

Howard Nedd
Contributor
April 30, 2021

Hi Martin,

 

Thank you.

This does not disable the scripted fields right? It will just make it return nothing, right?

So I believe the only option is getting rid of the scripted fields on the screens it is added to.

Martin Bayer _MoroSystems_ s_r_o__
Community Champion
April 30, 2021

Hi @Howard Nedd , yeah, it won't disable it, but if a null value is returned, the field should not be displayed on view screen...

Radek Janata
Contributor
September 13, 2023

Please bear in mind that removing the script field from screens will just hide the field from users but it doesn't ensure the script field won't be executed.

The script field is still executed when it's used as a column in the Issue Navigator, in JQL and other queries (board quick filters etc.), via exports, REST API calls (where fields are not explicitly specified) and probably (not sure) when an issue is re-indexed.

1 vote
Answer accepted
Nic Brough -Adaptavist-
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 30, 2021

"return null" on the first line is the easiest way to "disable" a scripted field.

But I think there is a deeper question here.

Why are you trying to "disable" a scripted field?  What do you get from "disable" that is better than "delete"?

Howard Nedd
Contributor
May 3, 2021

Hi Nic, valid question. 

The fields don't seem to be used right now. They have been added to 2 screens of our entire config and those screens are part of projects that no longer will be used but to make sure I dont mess things up I wanted to disable these fields.

The script itself was also giving out errors  (seems the script did not have an error handler so whenever the script resulted in "Not True" it gave a error). 

 

So if we do decide to keep this error I will add a the error handler to it but for now I will add return null to the scripts and eventually get rid of them if they are not needed.

Howard Nedd
Contributor
May 3, 2021

How would an error handler look if I wanted to add it to this script?

 

return null 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.project.Project
import java.text.SimpleDateFormat
import groovy.json.JsonSlurper
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.bc.project.property.ProjectPropertyService
import com.atlassian.jira.security.JiraAuthenticationContext
import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.user.util.UserManager

def customFieldManager = ComponentAccessor.getCustomFieldManager()

def sprintCf = customFieldManager.getCustomFieldObject(10000)
def sprints = issue.getCustomFieldValue(sprintCf)
//def sprint = sprints.find { !it.isClosed() }
//if (sprint == null)
def sprint = sprints?.last()
getSprintEndDate(issue.projectObject, sprint)

Date getSprintEndDate(Project project, def sprint) {
if (sprint) {
if(sprint.endDate){
return sprint.endDate.toDate()
} else {
def projectParams = getProjectParameters(project.id, 'sprint_settings')
def sprintSettings = new SprintSettings(projectParams)
String endDate = sprintSettings.getEndDate(sprint.id)
if(endDate) {
return new SimpleDateFormat('yy-MM-dd').parse(endDate)
}
}
}
return null;
}

Map getProjectParameters(Long projectId, def key) {
try {
JiraAuthenticationContext jiraAuthenticationContext = ComponentAccessor.jiraAuthenticationContext
ProjectPropertyService projectPropertyService = ComponentAccessor.getComponentOfType(ProjectPropertyService.class)
UserManager userManager = ComponentAccessor.userManager

def user = userManager.getUserByName(jiraAuthenticationContext.getLoggedInUser()?.name)
def value = projectPropertyService.getProperty(user, projectId, key)?.entityProperty?.value?.value
if(value) {
def ret = new JsonSlurper().parseText(value)
return ret
}
return [:]
} catch(Exception e) {
return [:]
}
}

class SprintSettings {

private List settings = []

def SprintSettings(Map jsonParameters) {
jsonParameters.settings.each {
settings << [
sprintId: it.sprint_id,
capacity: it.capacity,
startDate: it.start_date,
endDate: it.end_date
]
}
}

List getSettings() {
return settings
}

boolean contains(Long sprintId) {
return contains(sprintId as String)
}

boolean contains(String sprintId) {
return (settings.find { it.sprintId == sprintId } != null)
}

def getCapacity(def sprintId) {
return settings.find { it.sprintId == sprintId as String }?.capacity ?: 0
}

def getStartDate(def sprintId) {
return settings.find { it.sprintId == sprintId as String }?.startDate ?: ''
}
def getEndDate(def sprintId) {
return settings.find { it.sprintId == sprintId as String }?.endDate ?: ''
}
}
1 vote
Kevin Johnson
Community Champion
April 30, 2021

Hi @Howard Nedd ,

Sadly, I don't think there is a possibility to hide/disable the scripted field instead of deleting them.

-Kevin

Howard Nedd
Contributor
April 30, 2021

Hi Kevin, Thank you for getting back at me. I have searched the web and it seems there is no way to simply disable it to do your test, so the remaining option is documenting the script and delete it for now.

Like # people like this
Kevin Johnson
Community Champion
April 30, 2021

@Howard Nedd Yea, unfortunately, that's the only option for now.

-Kevin.

Suggest an answer

Log in or Sign up to answer