Update numerical custom field from string array

Peter Ellis November 23, 2022

Hi,

I am trying to do two things here. Firstly, I want to turn a string from an array into an numeric and then save that into a numeric custom field (Story Points). 

The script works perfectly other than the part above.

 

This is an example of the array,

def storyTypeTasks = 

["Summary": "Config - Verify Circuit", "Description": "Config - Verify Circuit", "Task Type": "Technical task", "Component":"Configuration Team", "Story Points": "15"],

 

import org.apache.log4j.Logger
import org.apache.log4j.Level

import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.component.ComponentAccessor

def log = Logger.getLogger("com.bttv.MDTStoryPostFunction")
log.setLevel(Level.DEBUG)

 

def constantManager = ComponentAccessor.getConstantsManager()
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

def issueFactory = ComponentAccessor.getIssueFactory()
def issueManager = ComponentAccessor.getIssueManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def subTaskManager = ComponentAccessor.getSubTaskManager()

// Get project Type field details
def storyTypeField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Story type")
log.debug "storyTypeField = " + storyTypeField

// Get story type for current story
Issue story = issue
def storyType = story.getCustomFieldValue(storyTypeField)
log.debug "storyType = '" + storyType + "'"

//get story points
def Storypointsfield = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Story Points")

// Find matching story type and tasks to create
def storyTypeTaskTemplate = storyTypeTasks.find{ it.key == storyType.getValue() }
log.debug "storyTypeTaskTemplate = '" + storyTypeTaskTemplate + "'"

 

// This story project type is templated
if(storyTypeTaskTemplate) {
log.debug "Processing template..."
log.debug "Story '" + story.summary + "' with type '" + storyType + "' has task template"
log.debug "storyTypeTaskTemplate: '" + storyTypeTaskTemplate + "'"
// Iterate thru list of tasks to create
storyTypeTaskTemplate.getValue().each {
log.debug "storyTypeTaskTemplate.each: '" + it + "'"

// Build issue
MutableIssue newTask = issueFactory.getIssue()
newTask.setSummary(it["Summary"])
newTask.setDescription(it["Description"])
newTask.setParentObject(story)
newTask.setProjectObject(story.getProjectObject())
newTask.setIssueTypeId(constantManager.getAllIssueTypeObjects().find{
it.getName() == "Sub-task"
}.id)

 

//////////////////////////////////////////////////////////

// THIS IS THE LINE THAT FAILS 

//Set Temp story Points
newTask.setCustomFieldValue("Story Points",it["Story Points"])

//////////////////////////////////////////////////////////


// Set component
def theComponent = ComponentAccessor.getProjectComponentManager().findByComponentName(story.getProjectObject().getId(),it["Component"])
newTask.setComponent([theComponent])

// Create task
def newTaskParams = ["issue" : newTask] as Map<String,Object>
Issue task = issueManager.createIssueObject(user, newTaskParams)

// Create subtask linkage
subTaskManager.createSubTaskIssueLink(story, task, user)
log.debug "Issue with summary ${task.summary} created"
}

// Not template for this type of epic
} else {
log.debug "Story '" + story.summary + "' with type '" + storyType + "' has NO task template"
}

1 answer

0 votes
Trudy Claspill
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 22, 2022

Hello @Peter Ellis 

Did you ever solve this issue?

Are you getting an error message? If so, please share that.

Suggest an answer

Log in or Sign up to answer