Forums

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

ScriptRunner: Scripted Field doesnt work

dD
Contributor
October 13, 2021

Hello Community,

I want to create a ScriptedField that sums up the number values of a custom field of all linked issues. Therefore I use this code: https://library.adaptavist.com/entity/calculate-the-sum-of-all-values-of-a-custom-field-in-linked-issues.

In applied the code to my project but I receive an error message: cannot return value of type java.lang.Object on method returning type java.lang.Double

The error message arises in the last line of the code ( linkedIssues*....)

Can you help me with this?

Best thanks in advance!

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue

// The issue type for which we want the scripted field to be displayed
final issueTypeName = 'SUD Lehrkraft Semester'

// The linked issues with that issue type will used
final linkedIssueType = 'Related'

// The values of that custom field - of type number - we want to sum up
final numberOfUsersFieldName = 'Modul SWS'

if (issue.issueType.name != issueTypeName) {
return null
log.info("IssueTypes do not match")
}

def linkedIssues = ComponentAccessor.issueLinkManager.getOutwardLinks(issue.id).findAll { it.destinationObject.issueType.name == linkedIssueType }
if (!linkedIssues) {
return null
log.info("No linked Issues found")
}

def numberOfUsersField = ComponentAccessor.customFieldManager.getCustomFieldObjects(linkedIssues.first().destinationObject).findByName(numberOfUsersFieldName)
if (!numberOfUsersField) {
log.debug "Custom field is not configured for that context"
return null
}

linkedIssues*.destinationObject.sum { Issue it -> it.getCustomFieldValue(numberOfUsersField) ?: 0 }

 

1 answer

1 accepted

1 vote
Answer accepted
Antoine Berry
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 13, 2021

Hi @dD ,

I would avise to use the custom field id instead of name, as you can have multiple custom fields with the same name (change the id with your own) : 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue

// The issue type for which we want the scripted field to be displayed
final issueTypeName = 'SUD Lehrkraft Semester'

// The linked issues with that issue type will used
final linkedIssueType = 'Related'

// The values of that custom field - of type number - we want to sum up
final numberOfUsersFieldNameId = 11001
final numberOfUsersFieldName = 'Modul SWS'

if (issue.issueType.name != issueTypeName) {
return null
log.info("IssueTypes do not match")
}

def linkedIssues = ComponentAccessor.issueLinkManager.getOutwardLinks(issue.id).findAll { it.destinationObject.issueType.name == linkedIssueType }
if (!linkedIssues) {
return null
log.info("No linked Issues found")
}

def numberOfUsersField = ComponentAccessor.customFieldManager.getCustomFieldObjects(linkedIssues.first().destinationObject).findByName(numberOfUsersFieldName)
if (!numberOfUsersField) {
log.debug "Custom field is not configured for that context"
return null
}

def custField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(numberOfUsersFieldNameId)
if (!custField){
return null
log.info("Custom field does not exist")
}

linkedIssues*.destinationObject.sum { Issue it -> it.getCustomFieldValue(custField) ?: 0 }

Let me know if that helped.

dD
Contributor
October 13, 2021

Hey @Antoine Berry , best thanks for your help!

I tried your code but it doesnt made a change, then I got a little confused so I changed it back. Also I checked the CustomFields and there is no other Field with the same name.

But I also progressed, as I could fix some errors. Now I receive Logs, which may help to understand the soruce of the problem. But I dont understand it. Maybe you can think of an soulution :) Anyway best thanks to you!!

error.GIF

here also my revised code:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue

// The issue type for which we want the scripted field to be displayed
final issueTypeName = 'SUD Lehrkraft Semester'

// The linked issues with that issue type will used
final linkedIssueType = 'SUD Modul'

// The values of that custom field - of type number - we want to sum up
final numberOfSWSFieldName = 'Modul SWS'

if (issue.issueType.name != issueTypeName) {
return null
}

def linkedIssues = ComponentAccessor.issueLinkManager.getInwardLinks(issue.id).findAll { it.sourceObject.issueType.name == linkedIssueType }
if (!linkedIssues) {
return null
}

def numberOfSWSField = ComponentAccessor.customFieldManager.getCustomFieldObjects(linkedIssues.first().sourceObject).findByName(numberOfSWSFieldName)
if (!numberOfSWSField) {
log.debug "Custom field is not configured for that context"
return null
}
log.info("configure cf worked")
linkedIssues*.sourceObject.sum { Issue it -> it.getCustomFieldValue(numberOfSWSField) ?: 0
log.info(it.getCustomFieldValue(numberOfSWSField))
}

  

Antoine Berry
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 14, 2021

Hi @dD ,

The correct method to retrieve a custom field value is with the id and not the name of it (as you can see in the javadoc the getCustomFieldValue method does not accepet a String).

Could you please try my code and add a screenshot of the error you are getting please.

dD
Contributor
October 14, 2021

Hey @Antoine Berry  you are right, it is best to use IDs.

Fortunately I have found a new script that works semi. By semi I mean: It gives me an error but also the correct answer. It sums up all linked issues which works fine.

I changed the method from name to id, like you advised me. It feels better, still I receive an error. 

import org.apache.log4j.Logger
import org.apache.log4j.Level
import com.onresolve.scriptrunner.runner.customisers.PluginModule
import com.atlassian.jira.issue.link.IssueLinkManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.Issue

@PluginModule
IssueLinkManager issueLinkManager
@PluginModule
CustomFieldManager customFieldManager


//--------------------------------------------------------------
// You must change the following variables as your needs
//--------------------------------------------------------------
final numericFieldNameId = 10301
//--------------------------------------------------------------

//Retrieve customfields from the system

def custField = customFieldManager.getCustomFieldObject(numericFieldNameId)


// Ensure source fields really exist
if(custField)
{
// Look for all linked issues
def inwardLinkedIssues = issueLinkManager.getInwardLinks(issue.id)*.sourceObject
def outwardLinkedIssues = issueLinkManager.getOutwardLinks(issue.id)*.destinationObject
def linkedIssues = inwardLinkedIssues + outwardLinkedIssues

return linkedIssues.sum{ Issue linkedIssue -> linkedIssue.getCustomFieldValue(custField) ?: 0 }
}

 My error message is here:

error2.GIF

Antoine Berry
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 14, 2021

The error is because the script cannot check the type of the custom field value, but in this case it should return a double if your field is a number. It should still execute if you try it as the error will be corrected on execution time.

Your new script follows the same logic (and uses the custom field id). It also retrieves both issue link types.

Like dD likes this

Suggest an answer

Log in or Sign up to answer