Forums

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

Set numeric custom field value from Script Listener

Jake Jollimore May 29, 2018

I'm trying to set the value of a custom numeric field using a Script Listener in Jira 7. 

 

My script is running, and not generating any errors, but it is not writing to the desired custom field. 

Right now it's just a preliminary test to figure out how to write to a custom field, so there's an arbitrary value in the below script. 

What am I doing wrong?

Note that I've verified that the condition in the last if statement is being met and the code is executing without error.

import com.atlassian.jira.bc.issue.IssueService;
import com.atlassian.jira.bc.issue.IssueService.UpdateValidationResult;
import com.atlassian.jira.bc.issue.IssueService.IssueResult;
import com.atlassian.jira.issue.IssueInputParameters;
import com.atlassian.jira.user.ApplicationUser;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.user.util.UserManager

def customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_10109");

IssueService issueService = ComponentAccessor.getIssueService();
IssueInputParameters issueInputParameters = issueService.newIssueInputParameters();

issueInputParameters.addCustomFieldValue(customField.getIdAsLong(), "123456");

ApplicationUser loggedInUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();
if (!loggedInUser) {
loggedInUser = UserManager.getUserByName('jira_bot')
}
UpdateValidationResult validationResult = issueService.validateUpdate(loggedInUser, event.issue.getId(), issueInputParameters);
if (validationResult.isValid()){
IssueResult result = issueService.update(loggedInUser, validationResult);
}

 

 

2 answers

1 accepted

1 vote
Answer accepted
Eric Lemay
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.
May 29, 2018

Your code looks sound.  You're absolutely certain the final condition result is valid?  Keep in mind that the issue Service will ignore some invalid values, so it may not explicitely be telling you of some errors.  Try to check the contents of validationResult.getErrorCollection() in your final if block in case there is anything there?  Alternatively, print the validationResult.getFieldValuesHolder() information to make sure that the values you're trying to set are properly getting saved.

That's where i'd start and go from there.

Jake Jollimore May 29, 2018

Yes, the condition is definitely being met. I even changes the custom field id to an incorrect one to ensure it would throw an error in that case (which it did).

I've already tried using log.info with validationResult.isValid() both inside and outside the if statement, and they both logged true. I also logged validationResult.getFieldValuesHolder(), and I just got the empty array string: "[ ]"

However, I haven't tried looking at getFieldValuesHolder(). I'll try that and post the results. Thanks!

Jake Jollimore May 29, 2018

I looked at the contents of the getFieldValuesHolder() and realized that my custom field wasn't showing up in there. I then realized that my custom field is behaving as a read-only field. 

I've yet to figure out why that's happening, but trying to modify a different custom numeric field worked with the same script, so it looks like the script works at least.

Thanks for recommending that method--it provided the info I needed to figure out what was going on.

0 votes
Danyal Iqbal
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.
May 29, 2018

 is it a postfunction or is it running in the script console?

def Cf2 = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_10109");  
def changeHolder = new DefaultIssueChangeHolder() textCf2.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(textCf2), myval),changeHolder)

try something like this to update the field

Suggest an answer

Log in or Sign up to answer