Forums

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

to automatically replenish Duedate by Severity when creating an issue

dengfuwen April 2, 2022

I want to implement a function with script runner: to automatically replenish Duedate by "Severity" when creating an issue

 Severity            Duedate

     S              Create Date + 2D

     A              Create Date + 5D

     B              Create Date + 10D

2 answers

1 accepted

0 votes
Answer accepted
Tuncay Senturk _Snapbytes_
Community Champion
April 4, 2022

Hi @dengfuwen 

As far as I understand, you are using Script Runner to update the issue's due date based on the value of a custom field. We need more information but here is the code that would help you as a start. The code would change based on the custom field type (text, dropdown, etc) and where you would use it (post function, listener, etc).

Anyway, I have to say that I haven't tested the code, but as I mentioned it might help you with your needs. Let me know if you need further assistance.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.event.type.EventDispatchOption

def mutableIssue = issue as MutableIssue
def customFieldManager = ComponentAccessor.customFieldManager
def issueManager = ComponentAccessor.issueManager
def user = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def SEVERITY_CUSTOMFIELD_ID = xxxxx // the long id value of the custom field

def dateToSet = issue.created

def severityCustomField = customFieldManager.getCustomFieldObject(SEVERITY_CUSTOMFIELD_ID)
def severityValue = issue.getCustomFieldValue(severityCustomField)

if (severityValue == "S") {
dateToSet = dateToSet + 2
} else if (severityValue == "A") {
dateToSet = dateToSet + 5
} else if (severityValue == "B") {
dateToSet = dateToSet + 10
}

mutableIssue.setDueDate(dateToSet)
issueManager.updateIssue(user, mutableIssue, EventDispatchOption.ISSUE_UPDATED, false)
dengfuwen April 5, 2022

Hi I tried to implement functionality with the code you provided but found error in last line of code error log below please help solve Thanks

log  :2022-04-06 10:55:00,269 ERROR [workflow.AbstractScriptWorkflowFunction]: Workflow script has failed for user 'fuwen.deng'. View here: https://jira.tclking.com/secure/admin/workflows/ViewWorkflowTransition.jspa?workflowMode=live&workflowName=Eagle-Lab+Bug+v4&descriptorTab=postfunctions&workflowTransition=1&highlight=1 groovy.lang.MissingPropertyException: No such property: EventDispatchOption for class: Script583 at Script583.run(Script583.groovy:25)

Tuncay Senturk _Snapbytes_
Community Champion
April 6, 2022

Agh! I forgot to add the import statement.

import com.atlassian.jira.event.type.EventDispatchOption

Also, I noticed that I have used createdDate in the last line, it should be dateToSet.

I've updated the above code accordingly.

dengfuwen April 6, 2022

Hi  

I changed the latest code for your generation and I still reported an error: issue = null

Please run the code in your jira environment and answer me again?I hope to do things right once can improve our efficiency.

Thank you!!

Logs:

2022-04-07 10:38:36,029 ERROR [workflow.AbstractScriptWorkflowFunction]: Workflow script has failed for user 'fuwen.deng'. View here: https://jira.tclking.com/secure/admin/workflows/ViewWorkflowTransition.jspa?workflowMode=live&workflowName=Eagle-Lab+Bug+v4&descriptorTab=postfunctions&workflowTransition=1&highlight=1
com.google.common.util.concurrent.UncheckedExecutionException: java.lang.NullPointerException: null value in entry: issue=null
at com.google.common.cache.LocalCache$Segment.get(LocalCache.java:2050)
at com.google.common.cache.LocalCache.get(LocalCache.java:3952)
at com.google.common.cache.LocalCache$LocalManualCache.get(LocalCache.java:4871)
at com.atlassian.jira.issue.customfields.persistence.EagerLoadingOfBizCustomFieldPersister.getValuesForIssueId(EagerLoadingOfBizCustomFieldPersister.java:114)
at com.atlassian.jira.issue.customfields.persistence.EagerLoadingOfBizCustomFieldPersister.getValuesForTypeAndParent(EagerLoadingOfBizCustomFieldPersister.java:98)
at com.atlassian.jira.issue.customfields.persistence.OfBizCustomFieldValuePersister.updateValues(OfBizCustomFieldValuePersister.java:158)
at com.atlassian.jira.issue.customfields.persistence.EagerLoadingOfBizCustomFieldPersister.updateValues(EagerLoadingOfBizCustomFieldPersister.java:67)
at com.atlassian.jira.issue.customfields.persistence.OfBizCustomFieldValuePersister.createValues(OfBizCustomFieldValuePersister.java:113)
at com.atlassian.jira.issue.customfields.persistence.EagerLoadingOfBizCustomFieldPersister.createValues(EagerLoadingOfBizCustomFieldPersister.java:55)
at com.atlassian.jira.issue.customfields.persistence.OfBizCustomFieldValuePersister.createValues(OfBizCustomFieldValuePersister.java:105)
at com.atlassian.jira.issue.customfields.persistence.EagerLoadingOfBizCustomFieldPersister.createValues(EagerLoadingOfBizCustomFieldPersister.java:49)
at com.atlassian.jira.issue.customfields.impl.AbstractSingleFieldType.createValue(AbstractSingleFieldType.java:144)
at com.atlassian.jira.issue.fields.ImmutableCustomField.createValue(ImmutableCustomField.java:701)
at com.atlassian.jira.issue.fields.ImmutableCustomField.updateValue(ImmutableCustomField.java:414)
at com.atlassian.jira.issue.fields.ImmutableCustomField.updateValue(ImmutableCustomField.java:400)
at com.atlassian.jira.issue.managers.DefaultIssueManager.updateFieldValues(DefaultIssueManager.java:716)
at com.atlassian.jira.issue.managers.DefaultIssueManager.updateIssue(DefaultIssueManager.java:667)
at com.atlassian.jira.issue.managers.DefaultIssueManager.updateIssue(DefaultIssueManager.java:653)
at com.atlassian.jira.issue.managers.RequestCachingIssueManager.updateIssue(RequestCachingIssueManager.java:217)
at com.atlassian.jira.issue.IssueManager$updateIssue$22.call(Unknown Source)
at Script670.run(Script670.groovy:25)
Caused by: java.lang.NullPointerException: null value in entry: issue=null
at com.google.common.collect.CollectPreconditions.checkEntryNotNull(CollectPreconditions.java:32)
at com.google.common.collect.SingletonImmutableBiMap.<init>(SingletonImmutableBiMap.java:42)
at com.google.common.collect.ImmutableBiMap.of(ImmutableBiMap.java:72)
at com.google.common.collect.ImmutableMap.of(ImmutableMap.java:124)
at com.atlassian.jira.issue.customfields.persistence.EagerLoadingOfBizCustomFieldPersister.lambda$getValuesForIssueId$1(EagerLoadingOfBizCustomFieldPersister.java:115)
at com.google.common.cache.LocalCache$LocalManualCache$1.load(LocalCache.java:4876)
at com.google.common.cache.LocalCache$LoadingValueReference.loadFuture(LocalCache.java:3528)
at com.google.common.cache.LocalCache$Segment.loadSync(LocalCache.java:2277)
at com.google.common.cache.LocalCache$Segment.lockedGetOrLoad(LocalCache.java:2154)
at com.google.common.cache.LocalCache$Segment.get(LocalCache.java:2044)
... 20 more

dengfuwen April 7, 2022

hI

dengfuwen April 10, 2022

Hi, my problem has not been solved, please continue to help me solve it, thank you

Tuncay Senturk _Snapbytes_
Community Champion
April 11, 2022

Hi,

If you are using this script in scripted post function, the issue can't be null. It is reserved and can be used in the post function without declaring.

Are you sure you're using scripted post function?

dengfuwen April 12, 2022

I want to set up the Duedate automatically through the postprocessing function of the workflow when creating new problems

dengfuwen April 12, 2022

post.png

Tuncay Senturk _Snapbytes_
Community Champion
April 12, 2022

Hmm, I think you're getting this error at the last line (while updating the issue). Are you using mutableIssue or issue there?

Can you share your latest code, please?

dengfuwen April 12, 2022

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.event.type.EventDispatchOption

def mutableIssue = issue as MutableIssue
def customFieldManager = ComponentAccessor.customFieldManager
def issueManager = ComponentAccessor.issueManager
def user = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def SEVERITY_CUSTOMFIELD_ID = 10859 // the long id value of the custom field

def dateToSet = issue.created

def severityCustomField = customFieldManager.getCustomFieldObject(SEVERITY_CUSTOMFIELD_ID)
def severityValue = issue.getCustomFieldValue(severityCustomField)

if (severityValue == "S") {
dateToSet = dateToSet + 2
} else if (severityValue == "A") {
dateToSet = dateToSet + 5
} else if (severityValue == "B") {
dateToSet = dateToSet + 10
}

mutableIssue.setDueDate(dateToSet)
issueManager.updateIssue(user, mutableIssue, EventDispatchOption.ISSUE_UPDATED, false)

dengfuwen April 12, 2022

The code I used was all that you helped with and I haven t made any changes.thanks

Tuncay Senturk _Snapbytes_
Community Champion
April 14, 2022

Hi @dengfuwen 

Sorry for the late response,

I just copied the code and used it in a workflow function and worked as expected.

Then I realized you're using this function while creating the ticket.

Please move down the function below to "Creates the issue originally."

It will be seen as below.

2022-04-14_10-39-38.png

dengfuwen April 14, 2022

Thanks for the reply,

There is another problem right now, I find that Duedate is wrong, whether "Severity" is set to S or B, Duedate is the creation time.Can the date not be added directly?

dengfuwen April 14, 2022

duedate .png

dengfuwen April 15, 2022

Hi 

I have this question is very urgent, please timely reply, thank you

Nic Brough -Adaptavist-
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.
April 15, 2022

Your code is only adding 2, 5 or 10 milliseconds to the date/time

For 2 days, try

dateToSet.plusDays(2)

or 

dateToSet = dateToSet + 172800000

dengfuwen April 17, 2022

My problem has been solved, thanks to the two brothers for your help

Expect the next problem can be solved quickly, because this time solution is too long

0 votes
Nic Brough -Adaptavist-
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.
April 2, 2022

What scripting or automation apps do you have?  (You'll need one to do this)

dengfuwen April 10, 2022

Hello, do you have any good way to help me?

Suggest an answer

Log in or Sign up to answer