Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,360,042
Community Members
 
Community Events
168
Community Groups

Post-function script Jira

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

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())
}

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)

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

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)

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.

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

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

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

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

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

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)

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

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

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

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

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)
}

Can you try

MutableIssue issue = issue

instead of 

MutableIssue issue
Issue issueIM  = issue  
WW Rising Star Aug 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 Aug 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

originally put question in answer field, moved to Reply

0 votes

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

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