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
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)
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)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
What scripting or automation apps do you have? (You'll need one to do this)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.