Snippets

Adaptavist Jira Cloud - Post Function - Increment Number Value

Created by Kristian Walker last modified
/*
* This example post function script must be run as the Iniating User and shows how to increment a number field by a specificed value . 
* "All right, title and interest in this code snippet shall remain the exclusive intellectual property of Adaptavist Group Ltd and its affiliates. Customers with a valid ScriptRunner
* license shall be granted a  non-exclusive, non-transferable, freely revocable right to use this code snippet only within their own instance of Atlassian products. This licensing notice cannot be removed or
* amended and must be included in any circumstances where the code snippet is shared by You or a third party." 
*/

def issueKey = issue.key

// Get custom fields
def customFields = get("/rest/api/2/field")
        .asObject(List)
        .body
        .findAll { (it as Map).custom } as List<Map>

// Lookup the Number field by its name
def numberFieldId = customFields.find { it.name == 'CustomFieldNameHere' }?.id 

// Specify the amount to increment the field by below
def incrementAmount = 1 

// If the number field is not empty add the valie
if (issue.fields[numberFieldId]) { 
    int currentValue = issue.fields[numberFieldId] 
    int updatedValue = (currentValue + incrementAmount)
    
 put("/rest/api/2/issue/${issue.key}") 
    // .queryString("overrideScreenSecurity", Boolean.TRUE) 
    .header("Content-Type", "application/json")
    .body([
        fields:[
                (numberFieldId): updatedValue        
                ]
    ])
    .asString()  
}else {
    // If the number field is empty just set it to the increment amount
 put("/rest/api/2/issue/${issue.key}") 
    // .queryString("overrideScreenSecurity", Boolean.TRUE) 
    .header("Content-Type", "application/json")
    .body([
        fields:[
                (numberFieldId): incrementAmount       
                ]
    ])
    .asString()  
}

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.