The Atlassian Community Forums are currently in read-only mode. We will be relaunching on a new platform on September 22 (read more here). We apologize for the extended downtime. For concerns or questions, please email communitymanagers@atlassian.com. See you on the other side, on the new Atlassian Community Forums! :)

×

Forums

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

ScriptRunner Field Behaviour to force a Comment if the Due Date is changed

Karl Samson
Contributor
May 1, 2024

Hi All, I'm trying to implement a Field Behaviour, using ScriptRunner, that will mandate the Comment field, only when the Due Date is Changed. I'm also using a JEP custom field to record the date that the Due Date is changed. 

However, the current script I'm running doesn't seem to distinguish between when the Due Date field is edited or not, and just makes the Comment field mandatory for every Edit of the issues, which ideally is not what I want!?

Can anybody help me fix my code please?

 

import java.text.SimpleDateFormat
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.component.ComponentAccessor
import java.sql.Timestamp
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField

Issue issue = getUnderlyingIssue();

def dateField = getFieldByName("Due Date");
def commentField = getFieldById("comments");

if(issue!=null){
   CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
   CustomField dateCustomField = customFieldManager.getCustomFieldObjectByName("Due Date");
   Timestamp dateCustomFieldTimestamp = issue.getCustomFieldValue(dateCustomField)!=null?(Timestamp)issue.getCustomFieldValue(dateCustomField):null;
   String dateCustomFieldValue = issue.getCustomFieldValue(dateCustomField)!=null? new SimpleDateFormat("MM/dd/yy").format(new Date(dateCustomFieldTimestamp.getTime())):"";
   String dateValue = dateField.value!=null?(dateField.value as Date).dateString:"";
   if(!dateValue.equalsIgnoreCase(dateCustomFieldValue)){
     commentField.setRequired(true);
   } else {
     commentField.setRequired(false);
   }
}

0 answers

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

DEPLOYMENT TYPE
SERVER
VERSION
9.12.7
TAGS
AUG Leaders

Atlassian Community Events