Issue in custom script

Fadi Shahwan November 14, 2024

Hello Everyone 

I'm using Jira DC v 8.22.2 and Im trying to achieve some requirements using script runner.

The script will update the Progress% of the "Demand" issue type based on the "Weight" and "Progress%" of the "milestone" sub tasks. However, I'm facing error (unable to resolve class) in  import com.atlassian.jira.issue.fields.CustomFieldManager

 

any idea about this error ? 

2 answers

2 accepted

1 vote
Answer accepted
Fadi Shahwan November 14, 2024

Hi @Bobby Bailey 

sure, HYG

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.util.IssueChangeHolder
import com.atlassian.jira.issue.fields.CustomFieldManager

// Get the current issue (demand)
Issue issue = issueManager.getIssueObject("ISSUE-KEY") // Replace with the actual issue key
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()

// Get sub-tasks
def subTasks = issue.getSubTaskObjects()
def totalWeight = 0
def weightedProgress = 0

subTasks.each { subTask ->
    def progress = subTask.getCustomFieldValue(customFieldManager.getCustomFieldObjectByName("progress%")) ?: 0
    def weight = subTask.getCustomFieldValue(customFieldManager.getCustomFieldObjectByName("weight")) ?: 0

    if (weight > 0) {
        weightedProgress += (progress * weight)
        totalWeight += weight
    }
}

// Calculate the final progress%
def finalProgress = totalWeight > 0 ? (weightedProgress / totalWeight) : 0

// Update the demand issue's progress%
def progressField = customFieldManager.getCustomFieldObjectByName("progress%")
issue.setCustomFieldValue(progressField, finalProgress)

// Save the changes
IssueChangeHolder changeHolder = new DefaultIssueChangeHolder()
issue.store() // Persist changes
Bobby Bailey
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.
November 14, 2024

Awesome, thank you!

So, to answer your initial question, the reason this was failing is because the import line is:

import com.atlassian.jira.issue.CustomFieldManager

Rather than 

import com.atlassian.jira.issue.fields.CustomFieldManager


But once I fixed that there were other issues with the script, and we can solve all of those and make it easier to write and read using HAPI. So, if I rewrite this script in HAPI:

 

def issue = Issues.getByKey('WEB-8') // Replace with the actual issue key

// Get sub-tasks
def issueSubtasks = issue.getSubTaskObjects()
def totalWeight = 0
def weightedProgress = 0

issueSubtasks.each { subtask ->

    def progress = subtask.getCustomFieldValue('progress%') ?: 0
    def weight = subtask.getCustomFieldValue('weight') ?: 0
    if(weight > 0){
        weightedProgress += (progress * weight)
        totalWeight += weight
    }
}

// Calculate the final progress%
def finalProgress = totalWeight > 0 ? (weightedProgress / totalWeight) : 0

// Update the demand issue's progress%
issue.update {
    setCustomFieldValue('progress%', finalProgress)
 }


Could you give this a try and let me know how you get on please?

Kind regards, 

Bobby

Fadi Shahwan November 14, 2024

Thanks for your support Bobby, i got one error related to the field name "Progress %" 

Could you check and advice 

Capture.PNG

 

Bobby Bailey
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.
November 14, 2024

Apologies, it looks like to update a number field you need to pass a string! (No idea why)

 

So this:

setCustomFieldValue('Custom Number Field', finalProgress)

becomes this:

setCustomFieldValue('Custom Number Field', finalProgress.toString())


Could you try this and let me know? And in the meantime I will try to find out why that is the case. 

Kind regards,

Bobby

Fadi Shahwan November 14, 2024

Perfect! it is working fine ;) 

Thanks a lot Bobby 

1 vote
Answer accepted
Bobby Bailey
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.
November 14, 2024

Hi @Fadi Shahwan , 

Are you able to share your script? It would make it much easier to see what the problem might be :-) 

Kind regards, 

Bobby

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
VERSION
8.22.2
TAGS
AUG Leaders

Atlassian Community Events