Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

How do I set a custom numeric field to zero in a ScriptRunner post-function script?

Tony Mackin August 19, 2020

I have a customer numeric field called  'minutes spent'.

When a new issue is created I want to set that field to zero,  in case the issue is actually cloned from another issue that already has some value in that field. 

 

I've created a post-function that will set the minutes spent field to null,  but would prefer to have it set to 0 so that the field will be displayed as zero when the new issue is viewed (and easily updated from the view screen  versus having to go into edit screen)

I set the default value for the field to be zero but that had no effect 

 

This script works to null out the field,  but again would rather set it to 0.

//Set fields to blank or null in case this issue is being cloned
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.*
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.MutableIssue
MutableIssue currentIssue = issue
customFieldManager = ComponentAccessor.getCustomFieldManager()
def fieldToSet1 = customFieldManager.getCustomFieldObjectsByName("Minutes Spent")[0]
def fieldConfig1 = fieldToSet1.getRelevantConfig(issue)
fieldToSet1.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(fieldToSet1), null), new DefaultIssueChangeHolder())

 

Search prior questions for a post that showed how to set a numeric field to a fixed value but couldn't find one.

1 answer

1 accepted

0 votes
Answer accepted
Najjar _Innovura_
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
August 20, 2020

Try the below, the script function should be after Create Issue Originally

//Set fields to blank or null in case this issue is being cloned
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.event.type.EventDispatchOption

IssueManager im = ComponentAccessor.getIssueManager()
CustomFieldManager cfm = ComponentAccessor.getCustomFieldManager()

def runAsUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
//if currentUser didnt work try this since customer might not have permission to updateValue
//def runAsUser = ComponentAccessor.getUserManager().getUserByName("any_user_with_jsd_group") //user should have permission to edit jira service desk tickets

def minuteSpentCf = cfm.getCustomFieldObjectsByName("Minutes Spent")[0]

issue.setCustomFieldValue( minuteSpentCf , new Long(0) )

im.updateIssue( runAsUser , issue , EventDispatchOption.DO_NOT_DISPATCH , false )
Tony Mackin August 20, 2020

Thanks -  I tried this and get the following error message.

(note I commented out the prior script lines... so line 46 is the im.updateIssue line)

 

2020-08-20 19:05:19,741 ERROR [workflow.AbstractScriptWorkflowFunction]: *************************************************************************************
2020-08-20 19:05:19,747 ERROR [workflow.AbstractScriptWorkflowFunction]: Script function failed on issue: WSC-1305, actionId: 1, file: null
java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Double
 at com.atlassian.jira.issue.customfields.impl.NumberCFType.getDbValueFromObject(NumberCFType.java:45)
 at com.atlassian.jira.issue.customfields.impl.AbstractSingleFieldType.updateValue(AbstractSingleFieldType.java:151)
 at com.atlassian.jira.issue.fields.ImmutableCustomField.updateValue(ImmutableCustomField.java:426)
 at com.atlassian.jira.issue.fields.ImmutableCustomField.updateValue(ImmutableCustomField.java:396)
 at com.atlassian.jira.issue.managers.DefaultIssueManager.updateFieldValues(DefaultIssueManager.java:728)
 at com.atlassian.jira.issue.managers.DefaultIssueManager.updateIssue(DefaultIssueManager.java:681)
 at com.atlassian.jira.issue.managers.DefaultIssueManager.updateIssue(DefaultIssueManager.java:667)
 at com.atlassian.jira.issue.managers.RequestCachingIssueManager.updateIssue(RequestCachingIssueManager.java:217)
 at com.atlassian.jira.issue.IssueManager$updateIssue$0.call(Unknown Source)
 at Script495.run(Script495.groovy:46)
Najjar _Innovura_
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
August 20, 2020
issue.setCustomFieldValue( minuteSpentCf , new Double(0) )
Najjar _Innovura_
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
August 20, 2020

I always mix up double and long 😂. Try the above. 

 

Sorry

Tony Mackin August 21, 2020

Thanks!   That fixed it.    Field set to 0  and always displays on the view screen now.

 

For reference,  here is the script that is working

/Set fields to zero or null in case this issue is being cloned
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.event.type.EventDispatchOption

IssueManager im = ComponentAccessor.getIssueManager()
CustomFieldManager cfm = ComponentAccessor.getCustomFieldManager()

def runAsUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
//if currentUser didnt work try this since customer might not have permission to updateValue
//def runAsUser = ComponentAccessor.getUserManager().getUserByName("any_user_with_jsd_group") //user should have permission to edit jira service desk tickets

def minuteSpentCf = cfm.getCustomFieldObjectsByName("Minutes Spent")[0]

issue.setCustomFieldValue( minuteSpentCf , new Double(0) )

im.updateIssue( runAsUser , issue , EventDispatchOption.DO_NOT_DISPATCH , false )
Like Najjar _Innovura_ likes this

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
VERSION
4.5.4
TAGS
AUG Leaders

Atlassian Community Events