Execute behavior only when specific field is changed

Samantha Webber March 14, 2017

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

5 votes
Answer accepted
Thanos Batagiannis _Adaptavist_
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.
March 14, 2017

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

Samantha Webber March 14, 2017

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.

 

Thanos Batagiannis _Adaptavist_
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.
March 14, 2017

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

Samantha Webber March 14, 2017

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.

Thanos Batagiannis _Adaptavist_
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.
March 14, 2017

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
Samantha Webber March 14, 2017

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

Samantha Webber March 14, 2017

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);
}
Like Batuhan ŞEN likes this
Thanos Batagiannis _Adaptavist_
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.
March 14, 2017

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

Samantha Webber March 14, 2017

Updated, thanks for the tip smile

Suggest an answer

Log in or Sign up to answer