Skip to main content
Example scripts
arrow icon
To homepage
Jira
Data centre icon
Data Center

Add Column to a New Structure View

Created 1 year ago, Updated 3 month(s) ago
App in script
ScriptRunner For Jira
ScriptRunner For Jira
by Adaptavist
Compatibility
compatibility bullet
Jira (7.7 - 8.6)
compatibility bullet
ScriptRunner For Jira (5.6.14)
Language |
groovy
import com.almworks.jira.structure.api.view.ViewSpecification
import com.almworks.jira.structure.api.permissions.PermissionLevel
import com.almworks.jira.structure.api.util.JsonMapUtil
import com.almworks.jira.structure.api.StructureComponents
import com.onresolve.scriptrunner.runner.customisers.PluginModule
import com.onresolve.scriptrunner.runner.customisers.WithPlugin

@WithPlugin("com.almworks.jira.structure")

@PluginModule
StructureComponents structureComponents

def viewManager = structureComponents.viewManager

// The view id to create the column in it
final viewId = 118
// Formula params can be extracted from the POST request payload captured while you save a new formula column using browser developer tools.
// Look for a POST request to <BASEURL>/rest/structure/1.0/view and find your column spec in the "Request Payload" section under the "Headers" tab.
final formula = [
    formula: "original_estimate * 2",
    normalizedFormula: "mul(original_estimate,2)",
    variables: [
        original_estimate: [id: "timeoriginalestimate", format: "duration"]
    ]
]

def existingView = viewManager.getView(viewId, PermissionLevel.NONE)
if (!existingView) {
    log.warn("View #'${viewId}' was not found")
    return
}

final sumOverSubItems = false

def columnParams = JsonMapUtil.copyParameters(formula, true, false, false)
if (sumOverSubItems) {
    columnParams.aggregate = 'sum'
    columnParams.forestSpec = 'unfiltered'
} else {
    columnParams.remove("aggregate")
    columnParams.remove("forestSpec")
}
// If you want Distinct flag support add a new function parameter and logic to set/remove it from columnParams
columnParams.remove("distinct")

try {
    def viewSpecBuilder = new ViewSpecification.Builder(existingView.specification)

    def columnBuilder = viewSpecBuilder.addColumn('formula')
    columnBuilder.name = 'My New Column'
    columnBuilder.parameters = columnParams

    def newView = viewManager.createView()
    newView.specification = viewSpecBuilder.build()
    // Set proper view name and description
    newView.name = "${existingView.name} v2.0"
    newView.description = "${existingView.description} with ${columnBuilder.name}"
    newView.owner = existingView.owner

    newView.makePublic().saveChanges()

    "New view with id ${newView.id} created."
} catch (Exception e) {
    log.warn("Unexpected error happened while processing view #${existingView.id} '${existingView.name}' Error: ${e}")
}
Having an issue with this script?
Report it here