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

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

This widget could not be displayed.
Jenna Davis Community Champion Dec 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

This widget could not be displayed.

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

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 

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

Suggest an answer

Log in or Sign up to answer
Atlassian Summit 2018

Meet the community IRL

Atlassian Summit is an excellent opportunity for in-person support, training, and networking.

Learn more
Community showcase
Published 9 hours ago in Marketplace Apps

The 7 hacks of highly successful automation

...there's anything I've learnt from working, it's that people are lazy! No offense to anyone reading this, but it's true and we can all admit it. The easier you make something for someone, the more...

52 views 0 8
Read article

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you