Custom Listener to not allow due date to be changed within 2 days

Patrick Rach December 20, 2017

Hello all. I am slowly learning all of this but I was wondering if someone could help me out here. I want to make it where if someone tries to create an issue or update an issue with an issue type of Update TCA and Update TruChart with the custom field of environment equaling Production, within 48 hours (2 days) of the current date, that they are NOT allowed to.

The following code does NOT give me any errors but it doesn't appear to be working either. I am currently creating this as a Custom Listener using  Adaptavist ScriptRunner. 

Any help would be MUCH appreciated! Thank you in advance.

-------------------MY CODE------------------------------------------------------------import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.Issue

def issue = event.issue as Issue

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
CustomField cf = customFieldManager.getCustomFieldObjectByName("Environment")

def issueType = issue.getIssueType()
def dueDate = issue.getDueDate()
Date today = new Date()
def daysBetween = dueDate - today


// validation due date is 2 days against Update Issue Types for production environments
if (issueType == "Update TruChart" || issueType == "Update TCA" && daysBetween < 2 && cf.getValue(issue) == "Production"){
return false
} else {
return true
}

 

ScriptRunner Code.JPG

2 answers

2 accepted

1 vote
Answer accepted
Patrick Rach January 2, 2018

Hey Jenna,

Thank you for your reply. I checked out the two links that you provided. So I am trying the behaviours way. Please let me know if I am in the right direction and any help would be much appreciated.

For the behaviours, I have the field as Due Date with my Workflow mapped, using a server-side script of this.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.Issue


CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
CustomField cf = customFieldManager.getCustomFieldObject("customfield_11392") //Environment Custom Field ID = 11392

def dd = getFieldById("duedate")
//def issueType = Issue.getIssueType()
def dueDate = Issue.getDueDate()
Date today = new Date()
def daysBetween = dueDate - today

// validation due date is 2 days against Update Issue Types for production environments
if (daysBetween < 2 && cf == "Production"){
dd.setReadOnly(true)
dd.setError("Due Date must be 48 hours in advance. Contact QA and IT Ops for within 48 hours.")
} else {
dd.setReadOnly(false)
dd.clearError()
}

I am getting the following error in the logs. I believe that something isn't being passed through. Any help would be great! Thank you in advance :)

[c.o.jira.behaviours.BehaviourManagerImpl] Script function failed on issue: issue: AST-17464, user: patrickr, fieldId: duedate, file: <inline script>
groovy.lang.MissingMethodException: No signature of method: static com.atlassian.jira.issue.Issue.getDueDate() is applicable for argument types: () values: []
 at Script1.run(Script1.groovy:14)
2018-01-02 15:53:48,480 ajp-nio-8009-exec-4890 ERROR patrickr 953x2161603x1 1cpojxy 65.124.137.10 /rest/com.onresolve.jira.plugin.Behaviours/1.0/behaviours/runvalidator.json

image.png

Jenna Davis
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.
January 3, 2018

In order to access the issue in a behaviour you need to use 'underlyingIssue', I believe this is causing the error you're seeing. I also fixed a problem later in your code so that you could get the value from your custom field and compare it to a string. This should get you pointed in the right direction, let me know if you need more help!

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
CustomField cf = customFieldManager.getCustomFieldObject("customfield_11392") //Environment Custom Field ID = 11392

def dd = getFieldById("duedate")
//def issueType = Issue.getIssueType()
def dueDate = underlyingIssue.dueDate
Date today = new Date()
def daysBetween = dueDate - today

// validation due date is 2 days against Update Issue Types for production environments
if (daysBetween < 2 && cf.getValue(underlyingIssue) as String == "Production"){
dd.setReadOnly(true)
dd.setError("Due Date must be 48 hours in advance. Contact QA and IT Ops for within 48 hours.")
} else {
dd.setReadOnly(false)
dd.clearError()
}

Jenna 

Patrick Rach January 3, 2018

Thank you for the help. It appears to be working partially now. It seems that I can create issues with the due date within or beyond 48 hours. (Should only allow ones that are longer than 48 hours. I can take care of the create validation just with a post-function script validator.

So, the next issue. However, if I take the created issue, edit it and change the date to within 48 hours the error message appears, the error message is there so I change the due date to outside 48 hours. The error message doesn't go away and I am unable to update the issue. Also, when I change my customfield value to anything but production, I am unable to update the issue too.

I think what is going on here is that the behaviour is looking at the current issue that has been created and it's current values instead of real time and me trying to update values.

This is why I was trying to do a Custom Listener in the beginning to catch all real time.

Would a Custom Listener be beneficial for this? I really appreciate your time in helping me. I am new to your plugin and just trying to gain how everything works. :)

Patrick

1 vote
Answer accepted
Jenna Davis
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.
December 20, 2017

Hello, 

I don't think you can use a listener to achieve this. I think you'd be able to do what you described through a behaviour instead though.

You can read about behaviours here:

https://scriptrunner.adaptavist.com/latest/jira/behaviours-overview.html

There are also several examples of different behaviours avaliable here:

https://scriptrunner.adaptavist.com/latest/jira/recipes/behaviours.html

Check out those pages and examples - If you need further help getting the code to work let me know! :)

Jenna

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events