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

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

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

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 )

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)
issue.setCustomFieldValue( minuteSpentCf , new Double(0) )

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

 

Sorry

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
Community showcase
Published in Jira Service Management

Why upgrade to Jira Service Management Premium?

We often have questions from folks using Jira Service Management about the benefits to using Premium. Check out this video to learn how you can unlock even more value in our Premium plan.  &nb...

162 views 0 4
Read article

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you