Execute behavior only when specific field is changed

Hi,

I want to mark a field as required only when the DueDate has been modified. I have this behavior in the "serverside script" for the DueDate field.

 

However, currently the behavior is executed when anything is modified, not just the DueDate. I've tried elaborate ways to overcome this, but there has to be something simpler. What is the effect of defining the behaviour under a particular field? How can I make my code execute only when one particular field is changed?

 

See code:

import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.component.ComponentAccessor
import org.apache.log4j.Category
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.ModifiedValue

 def reasonBox = getFieldById("customfield_11100");
 //clear reason box
 reasonBox.setFormValue(null);
 //require reason box so that user is forced to enter text
 reasonBox.setRequired(true);

1 answer

1 accepted

4 votes
Accepted answer

Hi Samantha,

So the script below will make the description required when the due date the user selects during edit  is different than the due date the issue already has.

def currentDueDate = underlyingIssue?.dueDate?.dateString

log.debug "Due date for the issue : $currentDueDate "

def dueDate = getFieldById(getFieldChanged())
def description = getFieldById("description")

log.debug "Field changed : $dueDate and it's value is ${dueDate.rawValue} "

if ((dueDate.value as Date).dateString != currentDueDate.toString()) {
    description.setRequired(true)
} 
else {
    description.setRequired(false)
}

This behaviour is assigned to the due date field. I would propose to set a log message and check the value the due date has every time you select one. 

Please let me know if this does the trick

regards, thanos

Hi Thanos,

 

Thanks very much for your answer!

 

This will set the description to required only if the due date equals a specific date. But I don't know what date it will be in advance. Do you know if there's any way to test if the due date is simply different to what it was before?

 

Basically I want it so that when the user is in Edit View and changes the DueDate, a text box field is required.

 

Hi Samantha 

I updated the answer above. 

What actually you have to do is compare dates. The underlyingIssue?.dueDate will actually give you the date that the issue has, if any, currently. 

And then the getFieldById(getFieldChanged()).value will change avery time you select a date in the edit screen. 

And then compare them. 

I will advise you to check the logs in order to make sure in what kind of format the dates are, even though I believe getting the dateString will do the trick, give it a go and let me know.

regards, Thanos

Hi Thanos,

 

Thanks, this is getting me very close. Can one specify a custom field in place of dueDate in 'underlyingIssue?.dueDate'? Because it appears to be a custom field in our instance of JIRA.

So,

If you want to get the value of a custom field with name First DateTime and type Date Time Picker the way is 

def cf = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("First DateTime")
def currentDueDate = (underlyingIssue?.getCustomFieldValue(cf) as Date)?.dateString

You're awesome! Thanks, that did the trick!

 

The only problem now is if someone changes the due date, and then changes it back, within the same edit view, the text field is still mandatory. If you have any ideas they're welcome. But in any case you have already helped me tremendously smile

Ah never mind, with the 'else' statement everything works perfectly laugh To anyone who might have a similar issue, here is my code:

 

import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.component.ComponentAccessor
import org.apache.log4j.Category
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.ModifiedValue
import org.apache.log4j.Logger
import org.apache.log4j.Level
//grab reason box
def reasonBox = getFieldById("customfield_11100");
//grab due date in edit view
def dueDate = getFieldById(getFieldChanged());
//grab current due date from database
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
CustomField currentDueDate = customFieldManager.getCustomFieldObject('customfield_10025');
String currentDueDateAsString = (underlyingIssue?.getCustomFieldValue(currentDueDate) as Date)?.dateString;
//see date was changed in edit view (note null safe operator [?] which allows dueDate to be null without throwing an exception)
if((dueDate?.value as Date)?.dateString != currentDueDateAsString) {
 //clear reason box
 reasonBox.setFormValue(null);
 //require reason box so that user is forced to enter text
 reasonBox.setRequired(true);   
}
else {
    reasonBox.setRequired(false);
}

Great,

Just a minor thing, I would suggest to use the null safe operator in your if statement, like 

if((dueDate?.value as Date)?.dateString != currentDueDateAsString) { 
	// rest of the script
}

so in case that an issue does not have a due date, to not break the behaviour.

regards, Thanos

Updated, thanks for the tip smile

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Nov 27, 2018 in Portfolio for Jira

Introducing a new planning experience in Portfolio for Jira (Server/DC)

In the past, Portfolio for Jira required a high degree of detail–foresight that was unrealistic for many businesses to   have–in   order to produce a reliable long-term roadmap. We're tur...

2,763 views 18 21
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