Forums

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

when we open any issue getting an error in jira server as Script field failed on issue: 'Issue ID'

Velkuri Balaji July 14, 2023

Hello Team,

We are using Jira server, recently we are getting bellow error continuesally if any user opened an issue, later on understood it is script failed on field and we have checked on script on the field the script is showing correct no changes made. we are using script runner plug in to set scripts on fields.

Error Message: 

ERROR auday 279x2393098x2 17anbvy  /secure/AjaxIssueEditAction!default.jspa [c.o.scriptrunner.customfield.GroovyCustomField] Script field failed on issue: WM-140324, field: Resolved By
groovy.lang.MissingMethodException: No signature of method: com.atlassian.jira.user.util.UserUtilImpl.getUserByKey() is applicable for argument types: (String) values: [adeshpande]
at Script59$_run_closure2.doCall(Script59.groovy:10)
at Script59.run(Script59.groovy:10)

Note: It's not showing any kind of error on application UI, the errors are running in Jira server logs only.

Could someone help on it ?

Thanks 

Balaji

2 answers

1 accepted

1 vote
Answer accepted
Ram Kumar Aravindakshan _Adaptavist_
Community Champion
July 15, 2023

Hi @Velkuri Balaji

From the error message you are getting, I suspect you are encountering this after upgrading your Jira instance to version 9 and above. Please confirm.

The reason is that your method has already been deprecated since Jira 7, and as of Jira 9, it has been removed from the API.

Instead of using this approach, I suggest you update your ScriptRunner plugin to the latest release and use the HAPI feature.

Using HAPI, you can get the user by key using:-

import com.adaptavist.hapi.jira.users.Users

Users.getByKey('key')

I hope this helps to solve your question. :-)

Thank you and Kind regards,

Ram

Velkuri Balaji July 17, 2023

Hi @Florian Bonniec  and @Ram Kumar Aravindakshan _Adaptavist_ 

Thank you for your quick response;

I am using below script, could please provide the right script for it;

import com.atlassian.jira.component.ComponentAccessor;

def historyManager = ComponentAccessor.getChangeHistoryManager()
def userUtil = ComponentAccessor.getUserUtil()

def changeItems = historyManager.getAllChangeItems(issue).findAll { changeItem ->
    changeItem.field == "status" && changeItem.toValues.find{ it.value == "Resolved"} && !(changeItem.fromValues.find{ it.value == "Resolved"})
}

def userid = changeItems.collect { userUtil.getUserByKey(it.userKey).name}
if(userid)
userid.last()
Kindly change the above script to right script and share it, so that I'll apply the same.
Your help on these matters highly appreciated.
Thanks in advance,
Balaji
Ram Kumar Aravindakshan _Adaptavist_
Community Champion
July 18, 2023

Hi @Velkuri Balaji

The approach you are using is not the approach that I would recommend. 

Please clarify what version of ScriptRunner you are currently using.

If it is version 7.11.0 and above, the HAPI feature is already available, and you can use the simple approach that I have provided in my previous comment.

Thank you and Kind regards,
Ram

Velkuri Balaji July 18, 2023

Hi @Ram Kumar Aravindakshan _Adaptavist_ 

We are using Adaptavist ScriptRunner version 8.4.0 and Jira version is 9.8.1

I request you to provide full script.

Thanks,

Balaji

Ram Kumar Aravindakshan _Adaptavist_
Community Champion
July 18, 2023

Hi @Velkuri Balaji

If you are using ScriptRunner 8.4.0, the HAPI feature is already available.

You can try something like this:-

import com.adaptavist.hapi.jira.issues.Issues
import com.adaptavist.hapi.jira.users.Users
import com.atlassian.jira.component.ComponentAccessor

def issue = Issues.getByKey('MOCK-1')

def historyManager = ComponentAccessor.changeHistoryManager
def changeItemList = historyManager.getAllChangeItems(issue)
def userList = []

changeItemList.findAll { changeItem ->
def currentStatus = changeItem.toValues.values()
currentStatus.findAll {
if (it == 'Done') {
userList << changeItem.userKey
}
}
}

log.warn Users.getByKey(userList.last()).name

Please note that the sample working code above is not 100% exact to your environment. Hence, you will need to make the required modifications.

I hope this helps to solve your question. :-)

Thank you and Kind regards,

Ram

Velkuri Balaji July 18, 2023

Hi @Ram Kumar Aravindakshan _Adaptavist_ 

Thank you for your time and efforts,

We are not aware of script knowledge, could you please make necessary changes to below script and provide the right script to apply.

 

import com.atlassian.jira.component.ComponentAccessor;

 

def historyManager = ComponentAccessor.getChangeHistoryManager()
def userUtil = ComponentAccessor.getUserUtil()

 

def changeItems = historyManager.getAllChangeItems(issue).findAll { changeItem ->
    changeItem.field == "status" && changeItem.toValues.find{ it.value == "Resolved"} && !(changeItem.fromValues.find{ it.value == "Resolved"})
}

 

def userid = changeItems.collect { userUtil.getUserByKey(it.userKey).name}
if(userid)
userid.last()

 

Kindly help on these changes, it's great help for us to work smoothly.

 

Regards,

Balaji

Ram Kumar Aravindakshan _Adaptavist_
Community Champion
July 18, 2023

Hi @Velkuri Balaji

I've already provided you with a working solution. As I have already mentioned:-

Please note that the sample working code above is not 100% exact to your environment. Hence, you will need to make the required modifications.

In the example I provided, I am using the Done status. However, in your case, you are using the Resolved status. 

All you have to do is change the status from the sample code I have provided from Done to Resolved and ensure you update the issue variable accordingly as shown below:-

import com.adaptavist.hapi.jira.issues.Issues
import com.adaptavist.hapi.jira.users.Users
import com.atlassian.jira.component.ComponentAccessor

def historyManager = ComponentAccessor.changeHistoryManager
def changeItemList = historyManager.getAllChangeItems(issue)
def userList = []

changeItemList.findAll { changeItem ->
def currentStatus = changeItem.toValues.values()
currentStatus.findAll {
if (it == 'Resolved') {
userList << changeItem.userKey
}
}
}

Users.getByKey(userList.last().toString()).username

I hope this solves your question. :-)

Thank you and Kind regards,

Ram

Velkuri Balaji July 18, 2023

Hi @Ram Kumar Aravindakshan _Adaptavist_ 

 

Yes, I have made the change as you mentioned, but we are still getting. below error

 

2023-07-18T05:57:29,169 ERROR [customfield.GroovyCustomField]: ************************************************************************************* 2023-07-18T05:57:29,169 ERROR [customfield.GroovyCustomField]: Script field failed on issue: JIRA-194, field: Resolved By com.adaptavist.hapi.jira.issues.exceptions.IssueRetrievalException: The following errors occurred: (The issue no longer exists.) at com.adaptavist.hapi.jira.issues.implementation.IssuesImplementation.getByKey(IssuesImplementation.groovy:258) ~[?:?] at com.adaptavist.hapi.jira.issues.implementation.IssuesImplementation$getByKey.call(Unknown Source) ~[?:?] at com.adaptavist.hapi.jira.issues.Issues.getByKey(Issues.groovy:66) ~[?:?]

Ram Kumar Aravindakshan _Adaptavist_
Community Champion
July 18, 2023

Hi @Velkuri Balaji

Please share a screenshot of your Scripted Field configuration so I can better understand your configuration.

I am requesting this because I am using the ScriptRunner configuration below and it is working just fine for me.

scripted_field.png

Below is a test screenshot for your reference:-

Once the issue transitions to done, the last user is displayed on the Scripted Field.

test1.png

 

Another point from the latest error log you have provided shows that the issue does not exist anymore, which means that the issue could have been deleted.

However, if the issue does exist, please try fully re-indexing your issue once before running the test.

Thank you and Kind regards,
Ram

Velkuri Balaji July 18, 2023

Hi @Ram Kumar Aravindakshan _Adaptavist_ 

Thank you so much its' worked, I have updated as above mentioned in above screen shot, but it has to show 'Resolved by' field as user id for exmaple if my user id is vbalaji then it has to show 'Resolved By vbalaji' 

However, after updating above script we are seeing resolved by as 33629 instead of user id.

 image.png

Ram Kumar Aravindakshan _Adaptavist_
Community Champion
July 18, 2023

Hi @Velkuri Balaji

The number that it displays is the user id.

Are you trying to display the user's username? If yes, change it to:-

Users.getByKey(userList.last().toString()).username 

I hope this helps solve your question. :-)

Thank you and Kind regards,
Ram 

Velkuri Balaji July 18, 2023

Hi @Ram Kumar Aravindakshan _Adaptavist_ 

Thank you so much for your time and efforts, it's working very well!

Regards,

Balaji

Ram Kumar Aravindakshan _Adaptavist_
Community Champion
July 18, 2023

Hi @Velkuri Balaji

In your latest comment, you mentioned:-

After updating above script, we are getting below error, it seems like if any issue doesn't have "resolved by" status it's throwing an error.

2023-07-19T01:08:32,985 ERROR [customfield.GroovyCustomField]: ************************************************************ 2023-07-19T01:08:32,985 ERROR [customfield.GroovyCustomField]: Script field failed on issue: JIRA-193, field: Resolved By java.util.NoSuchElementException: Cannot access last() element from an empty List at Script17.run(Script17.groovy:16) ~[?:?] 

Have you added the Resolved By into the if condition?

Currently, the if condition is only set to verify if the status is Resolved, shown below:-

......

if (it == 'Resolved') {

Please clarify if Resolved By is also a status or a field to identify the user.

Thank you and Kind regards,

Ram

Velkuri Balaji July 18, 2023

Hi @Ram Kumar Aravindakshan _Adaptavist_ 

Thank you for quick response,

'Resolved By' is a field and the script we are updating to resolved by field only,

Actually, In our case we have resolved status for some workflows, and it's associated with few projects and rest projects workflow doesn't have resolved status.

Therefore, we would like to see the resolved by field if status has changed to resolved now that is working fine, however when we open any issue which doesn't have resolved status we are getting above error.

I hope you understand the requirement.

Regards,

Balaji

Ram Kumar Aravindakshan _Adaptavist_
Community Champion
July 19, 2023

Hi @Velkuri Balaji

In your last comment, you mentioned:-

Actually, In our case we have resolved status for some workflows, and it's associated with few projects and rest projects workflow doesn't have resolved status.

Therefore, we would like to see the resolved by field if status has changed to resolved now that is working fine, however when we open any issue which doesn't have resolved status we are getting above error.

From what I have read, I suspect you have configured the Scripted Field to be Global Context and not to the specific project(s) that have the Resolved status. Hence, you are encountering this problem.

It would be best to change the project from being configured as Global Context to only the Projects that actually use that status.

The alternative I can suggest, but I cannot guarantee it will work, is to add another filter in the code to restrict it to the specific workflows.

Something like:-

import com.adaptavist.hapi.jira.users.Users
import com.atlassian.jira.component.ComponentAccessor

def historyManager = ComponentAccessor.changeHistoryManager
def changeItemList = historyManager.getAllChangeItems(issue)
def userList = []
final def statusName = 'Resolved'

def workflowManager = ComponentAccessor.workflowManager
def workflow = workflowManager.getWorkflow(issue)
def resolvedStatus = workflow.linkedStatusObjects.findByName(statusName)

if (workflow.linkedStatusObjects.contains(resolvedStatus)) {
changeItemList.findAll { changeItem ->
def currentStatus = changeItem.toValues.values()
currentStatus.findAll {
if (it == statusName) {
userList << changeItem.userKey
}
}
}
Users.getByKey(userList.last().toString()).name
}

I hope this helps to solve your question. :-)

Thank you and Kind regards,

Ram

1 vote
Florian Bonniec
Community Champion
July 14, 2023

Hi @Velkuri Balaji 

 

It seems that the value you are entering in getUserByKey is an array an not a String.

Without the script it's hard to guide you but I would say that at line 10 you have something like

getUserByKey(variable) where variable is not a string, you could use variable?.first().toString()

 

Regards

Lukasz Grobelny
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.
July 14, 2023

Yeah, you need to pull the value from the array, because instead of adeshpande, you are passing [adeshpande] 

Velkuri Balaji July 18, 2023

Hi @Florian Bonniec 

 

We are using script runner v 8.4.0 and Jira is 9.8.1, please check the below script and provide the right script with changes;

 

import com.atlassian.jira.component.ComponentAccessor;

def historyManager = ComponentAccessor.getChangeHistoryManager()
  def userUtil = ComponentAccessor.getUserUtil()

def changeItems = historyManager.getAllChangeItems(issue).findAll { changeItem ->
    changeItem.field == "status" && changeItem.toValues.find{ it.value == "Resolved"} && !(changeItem.fromValues.find{ it.value == "Resolved"})
}

def userid = changeItems.collect { userUtil.getUserByKey(it.userKey).name}
if(userid)
userid.last()
Thanks in advance,
Balaji
Velkuri Balaji July 18, 2023

Hi @Ram Kumar Aravindakshan _Adaptavist_ 

After updating above script, we are getting below error, it seems like if any issue doesn't have "resolved by" status it's throwing an error.

2023-07-19T01:08:32,985 ERROR [customfield.GroovyCustomField]: ************************************************************ 2023-07-19T01:08:32,985 ERROR [customfield.GroovyCustomField]: Script field failed on issue: JIRA-193, field: Resolved By java.util.NoSuchElementException: Cannot access last() element from an empty List at Script17.run(Script17.groovy:16) ~[?:?] 

Could you please help on above issue.

Regards,

Balaji

Lukasz Grobelny
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.
July 18, 2023

This is different type of field, it's not getting an array

Velkuri Balaji July 19, 2023

Hi @Lukasz Grobelny 

'Resolved By' is a field and the script we are updating to resolved by field only,

Actually, In our case we have resolved status for some workflows, and it's associated with few projects and rest projects workflow doesn't have resolved status.

Therefore, we would like to see the resolved by field if status has changed to resolved now that is working fine, however when we open any issue which doesn't have resolved status we are getting above error.

I hope you understand the requirement.

Regards,

Balaji

Lukasz Grobelny
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.
July 19, 2023

Oh, not an array, you are getting an empty list, sorry. So no value is being stored.

Suggest an answer

Log in or Sign up to answer