groovy saving values into database

We have a groovy script that we use in the post functions of the create transition for setting values to hidden fields. The values show up on the screen, but are not being saved in the database. If you go into the edit screen, the values show as none.

We are using JIRA 6.4 and the updated Groovy script module

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.customfields.view.CustomFieldParams
import com.atlassian.jira.issue.transport.CollectionParams
import com.atlassian.jira.issue.customfields.option.Option
import com.atlassian.jira.issue.customfields.impl.CascadingSelectCFType
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.util.ImportUtils
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.fields.CustomFieldImpl
import com.atlassian.jira.issue.fields.layout.field.FieldLayoutItem
//import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.event.type.*
//MutableIssue issue = ComponentAccessor.getIssueManager().getIssueObject('PRD-3876')
ComponentAccessor componentAccessor = new ComponentAccessor()
CustomFieldManager customFieldManager = componentAccessor.getCustomFieldManager()
IssueManager issueManager = componentAccessor.getIssueManager()
CustomField customField = customFieldManager.getCustomFieldObjectByName("Type : Sub Type")
Object value = issue.getCustomFieldValue(customField)

HashMap<String, Option> hashMapEntries = (HashMap<String, Option>) value
if (hashMapEntries != null) 
	{
	parent = hashMapEntries.get(CascadingSelectCFType.PARENT_KEY)
	child = hashMapEntries.get(CascadingSelectCFType.CHILD_KEY)
	}
CustomField insParent = customFieldManager.getCustomFieldObjectByName("Type-Type")
CustomField insChild = customFieldManager.getCustomFieldObjectByName("Subtype-Subtype")

DefaultIssueChangeHolder changeHolder = new DefaultIssueChangeHolder()
def modifiedValueP = new ModifiedValue(issue.getCustomFieldValue(insParent), parent)
def modifiedValueC = new ModifiedValue(issue.getCustomFieldValue(insChild),child)

insParent.updateValue(null, issue, modifiedValueP, changeHolder)

insChild.updateValue(null, issue, modifiedValueC, changeHolder)

2 answers

Sorry for asking, but if the field is Hidden how do you see it on the screen? This may be related to another feature of JIRA where you can't modify a field via the API if it is Hidden - at least it was a feature a few versions ago.

I made it un-hidden so I could see the values. Normally it is hidden so we can use the two fields to query on. The custom field is a cascading field to organize issues. We split it out into two other fields to query on. We can see the information on the screen, but in the edit it says none and if you save it, it will set the values to the two fields to none. For some reason, we can't get it to save in the database.

You should try to save the modified issues using issueManager.

It is very likely that updateValue() will only set the value in the object, but will not persist that to the database. I'd reckon that issueManager provides a save(), update() or similar method to persist the changes. (I don't have the JIRA code here with me, so I can't give you the specifics, but it may give a hint where to start.)

I put this at the end of the script, but it still doesn't save the issue into the database. issueManager.updateIssue(null, issue, EventDispatchOption.ISSUE_UPDATED, true)

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Nov 29, 2018 in Jira

How to set up an incident workflow from the VP of Engineering at Sentry

Hey Atlassian community, I help lead engineering at Sentry, an open-source error-tracking and monitoring tool that integrates with Jira. We started using Jira Software Cloud internally last year, a...

1,095 views 0 8
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you