Post-function script Jira

Alexander September 19, 2018

Good day!
Sorry for my bad English)
I wrote the script in post-function, but he is not work.
We need to change the custom field to the value of the system field, and if the custom field is already filled, then put its value in the system. I hope I have expressed clearly :)
Script:
import com.atlassian.jira.component.ComponentAccessor
def bf = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Исполнитель")
def br = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Технолог");
if(br) {
return issue.getCustomFieldValue(bf)
}  else{
 ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Исполнитель")
     return issue.getCustomFieldValue(br)
}

Error:

2018-09-19 12:17:55,896 ERROR [workflow.ScriptWorkflowFunction]: *************************************************************************************
2018-09-19 12:17:55,897 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: PB-420, actionId: 331, file: <inline script>
java.lang.NullPointerException
  at com.atlassian.jira.issue.IssueImpl.getCustomFieldValue(IssueImpl.java:896)
  at com.atlassian.jira.issue.Issue$getCustomFieldValue$1.call(Unknown Source)
  at Script151.run(Script151.groovy:5)

Please help me fix the script...
Thank you in advance!

3 answers

1 accepted

0 votes
Answer accepted
Orkun Gedik
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.
September 19, 2018

Hello @Alexander,

Can you try the code below. Let me know the result.

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

Issue issue = issue
CustomField customfield_a = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("cf id with value A")
CustomField customfield_b = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("cf id with value B")

def value_a = customfield_a.getValue(issue)
def value_b = customfield_b.getValue(issue)

if(value_a == null ||value_a == ""){
customfield_a.updateValue(null, issue, new ModifiedValue(null, value_b), new DefaultIssueChangeHolder())
}else{
customfield_b.updateValue(null, issue, new ModifiedValue(null, value_a), new DefaultIssueChangeHolder())
}
Alexander September 20, 2018

Thanks for your version!
Error log:

2018-09-20 14:44:00,513 ERROR [workflow.ScriptWorkflowFunction]: ************************************************************************************* 2018-09-20 14:44:00,514 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: PB-426, actionId: 331, file: <inline script> java.lang.NullPointerException: Cannot invoke method getValue() on null object at Script437.run(Script437.groovy:12)

Orkun Gedik
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.
September 20, 2018

I guess customfield B is null in your case. Make sure you are writing customfield id correctly. For intance, "customfield_1234". Can you please copy the following code again and share the logs again with the logs I added.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import org.apache.log4j.Logger

Logger log = Logger.getLogger("Atlassian Community")

Issue issue = issue
CustomField customfield_a = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("cf id with value A")
CustomField customfield_b = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("cf id with value B")

log.warn("Issue : " + issue)
log.warn("cf A : " + customfield_a)
log.warn("cf B : " + customfield_b)

def value_a = customfield_a.getValue(issue)
def value_b = customfield_b.getValue(issue)

log.warn("Value A : " + value_a)
log.warn("Value B : " + value_b)

if(value_a == null ||value_a == ""){
customfield_a.updateValue(null, issue, new ModifiedValue(null, value_b), new DefaultIssueChangeHolder())
}else{
customfield_b.updateValue(null, issue, new ModifiedValue(null, value_a), new DefaultIssueChangeHolder())
}
Like Brian Pryor likes this
Alexander September 20, 2018

log:

2018-09-20 15:12:23,819 WARN [Atlassian Community]: Issue : PB-428 2018-09-20 15:12:23,822 WARN [Atlassian Community]: Технолог : null 2018-09-20 15:12:23,822 WARN [Atlassian Community]: Разработчик : null 2018-09-20 15:12:23,827 ERROR [workflow.ScriptWorkflowFunction]: ************************************************************************************* 2018-09-20 15:12:23,829 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: PB-428, actionId: 331, file: <inline script> java.lang.NullPointerException: Cannot invoke method getValue() on null object at Script473.run(Script473.groovy:18)

Orkun Gedik
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.
September 20, 2018

Your customfields are null. Therefore you are getting null point exception while you are trying to get the value of a null object. The problem is in these lines.

CustomField customfield_a = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("cf id with value A")
CustomField customfield_b = ComponentAccessor.
getCustomFieldManager().getCustomFieldObject("cf id with value B")

Are you sure you are passing the correct paramater in getCustomFieldObject(). Can you share the lines that getting the both customfield objects in your script.

Alexander September 20, 2018

I wrote there ID fields ("custom field_14981") all right?

Orkun Gedik
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.
September 20, 2018

Do not seperate customfield. Write the ids as "customfield_1481"

Alexander September 20, 2018

Sorry. The text from the translator was inserted incorrectly. The script is written customfield_14981.

Alexander September 20, 2018

Maybe try to get a field by name instead of by ID?
CustomField cfReleaseVersion = customFieldManager.getCustomFieldObjectByName("Value_A")

Orkun Gedik
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.
September 20, 2018

You are right, you also can use getCustomFieldObjectsByName(). 

Alexander September 20, 2018

Wow) new error:
2018-09-20 15:59:08,852 ERROR [workflow.ScriptWorkflowFunction]: ************************************************************************************* 2018-09-20 15:59:08,853 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: PB-430, actionId: 331, file: <inline script> groovy.lang.MissingMethodException: No signature of method: static com.atlassian.jira.issue.CustomFieldManager.getCustomFieldObjectByName() is applicable for argument types: (java.lang.String) values: [Технолог] at Script506.run(Script506.groovy:9)

Orkun Gedik
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.
September 20, 2018

First, you need to get the CustomFieldManager class. Call the method as follows,

CustomField cfReleaseVersion = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Value_A")
Alexander September 20, 2018

Yyyyeeeehhh)))) Works! :) Thank you very much!)

Orkun Gedik
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.
September 20, 2018

You are welcome, glad to help you. Thank you for accepting answer :)

Alexander September 20, 2018

Hello again! :)
Another problem. What if one of the Assignee fields?
I suspect updating custom field values is not correct(
My script looks like this:

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

MutableIssue issue
Issue issueIM  = issue   
CustomField technolog = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Технолог")


def value_a = technolog.getValue(issue)
def assignee = issue.getAssigneeId()


if(value_a == null ||value_a == ""){
    technolog.updateValue(null, issue, new ModifiedValue(null, assignee), new DefaultIssueChangeHolder())
}
else {
 issue.setAssignee((ApplicationUser)value_a)
}

Orkun Gedik
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.
September 21, 2018

Can you try

MutableIssue issue = issue

instead of 

MutableIssue issue
Issue issueIM  = issue  
WW
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.
August 26, 2022

So, it's like 4 years later, but I'm now having this same problem that Alexander had. Let me know if you think I should just create a new question.

Granted, they've deprecated getCustomFieldObjectByName, but I can still use it. The alternative is not working either.

Overall, I need update a multi-user picker field with an additional user and don't want to overwrite the existing value.

For these snippets and the error I'm getting, I'm just trying to get the existing value of a multi-user picker field.

I tried a few things. This example was from this particular Q&A.

CustomField cfReleaseVersion = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Value_A")

log.info cfReleaseVersion.getName()

which errors with:

Cannot invoke method getName() on null object

That eliminates my next code block from being an error with the field name.

When I tried what I really wanted to do:

String fieldName = "Additional Assignees"
def customFieldManager = ComponentAccessor.getCustomFieldManager()

CustomField customField = customFieldManager.getCustomFieldObjectByName(fieldName)

log.info  customField.getName()

def oldValue = []
if(customField) {oldValue = issue.getCustomFieldValue(customField)}
log.info "Line 53"

It never gets to Line 53, but gives me this error:

Cannot invoke method getCustomFieldValue() on null object

which is the same thing that was in this question.

The log prints "Additional Assignees", so I know the field name is right. Otherwise, I'd get a getName() error.

If I try this, I get the same error and no custom log entry (the rest is the same as above):

CustomField customField = customFieldManager.getCustomFieldObjectByName(fieldName) 

log.info issue.getCustomFieldValue(customField)

I'm also checking for a value in customField before I try to get a value. There are no other fields with the same name.

The line it's erroring on matches the next to last line, and getCustomFieldValue() is not used anywhere else in the script.

I even put a value in the Additional Assignees field in the test issue I'm using.

My issue is a Mutable issue.

Are multi-user picker fields special? From what I've done in the past, they seem to work pretty much like text fields. Do I have something out of place?

WW
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.
August 29, 2022

Never mind. I figured it out.

The "issue" was null.

I was trying to pass the issue to a custom REST Endpoint for a Web item Fragment and realized I can't pass an object through a REST API call. So, I changed it to issuekey, got the object from the key, and it worked.

0 votes
WW
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.
August 26, 2022

originally put question in answer field, moved to Reply

0 votes
Orkun Gedik
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.
September 19, 2018

Hello @Alexander,

I did not understand what you are trying yo do in the postfunction. Could you please explain a bit more.

Regards,

Orkun Gedik

Alexander September 19, 2018

Hello!
There is a field with the value " A"
There is a field with a value of " B"
So I need, if the field with the value "A" is empty, then assign it the value "B". And if the field with the value " A "is already filled, assign this value to the field with the value"B".

Like AKumar47 likes this

Suggest an answer

Log in or Sign up to answer