Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

How can i get the original value of a CUSTOM field using Jira Behaviors?

Andrew L August 14, 2017

Hi,

Can someone provide some code snippet for Jira Behaviors plugin that shows how to retrieve the original value of a custom field?

I need to compare against the original value to see if the field has been changed and take some action if necessary.

Thanks

--Andrew

 

 

4 answers

Suggest an answer

Log in or Sign up to answer
0 votes
Aleksandr Gribukov February 18, 2022

Hi.
This is the way I used to change the DueDate.

import java.text.SimpleDateFormat
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

@BaseScript FieldBehaviours fieldBehaviours

Date oldDueDate = underlyingIssue.getDueDate()
Date newDueDate = new SimpleDateFormat("dd/MM/yy").parse(getFieldById("duedate").getFormValue().toString())
if (oldDueDate != newDueDate) {
getFieldById("comment").setRequired(true)
} else {
getFieldById("comment").setRequired(false)
}
0 votes
Alvin
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.
November 13, 2018

Hi Joshua Yamdogo @ Adaptavist, how can I revert back the original value if the custom field has set to blank/null in a group picker field?

0 votes
April August 15, 2017

If I may be so bold,

Why don't you simply create a transition for this?

If this field is required at creation, then put it on the Create and View screens only, and leave it off the Edit screen.

The tranisition could be from all to all, called something like "Update MyField," and you can put this field and the comment field on the screen for this transtion.

Then simply require Comment on the transition using a validator.

Would this cover your requirement?

Andrew L August 16, 2017

Thank you for the suggestion, April.

I think this is a bit much for my requirements.

The field is required during Create as well as Edit.

Thanks

--Andrew

0 votes
Joshua Yamdogo @ 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.
August 15, 2017

Does this need to be accomplished with behaviours? It sounds like this would be more suited to be done in a listener. For example, you can make a listener that fires on an Issue Created or Issue Updated Event (or all Issue Events). If someone updated the Priority field, check to see what the original value was and what it was changed to. Then, you can perform additional actions based off that information.

def change = event?.getChangeLog()?.getRelated("ChildChangeItem")?.find{ it.field=='priority' }
if (change) {
def oldValue = change.oldstring
def newValue = change.newstring
log.debug ("Value changed from ${oldValue} to ${newValue}")

// your actions if the field has changed
}

Log statement:

DEBUG admin [onresolve.scriptrunner.runner.ScriptRunnerImpl] Value changed from Low to High
Andrew L August 15, 2017

Hi

The requirement is to make the Comment field mandatory if the user changes the custom field.

Aren't Listeners a bit too after-the-fact for my requirements?

Thanks

--Andrew

Joshua Yamdogo @ 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.
August 15, 2017

Yes, you're correct that listeners wouldn't be very useful in that case. However, your question seemed focus on getting the specific value before and after the field was changed. I don't see why that would be needed if all you want to do is make the comment field mandatory based on a custom field value.

You'd just need a behaviour that would continually check the value of the custom field. It would run on the Create Issue, Update/Edit Issue, Assign Issue, and Workflow Transition screens. If it sees that the custom field has the wrong value, make the comment field mandatory.

def customField = getFieldByName('YourCustomFieldName')
def commentField = getFieldById("comment")

if (customField.getValue() != "Some value") {
commentField.setRequired(true)
}
else {
commentField.setRequired(false)
}

If the user changes the custom field to something it's not supposed to be, make the comment field required. Else, don't make the comment field required.

Andrew L August 15, 2017

We have a custom field that is a pull-down, so the user can only choose from one of the provided choices.

I need to know if the user selected a different choice of this custom field.  If so, make the Comment field mandatory when the user clicks on "Edit" and changes that custom field.  The Comment field would then be required before the user can click on "Update" to save.

Joshua Yamdogo @ 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.
August 16, 2017

Hi Andrew,

For example:

  • Select List has values A, B, C
  • A is the correct value.
  • If the user clicks "Edit" and changes the custom field and chooses B or C, the comment field will be required.
  • The user will not be able to click "Update" until they provide a comment.
def customField = getFieldByName('YourCustomFieldName')
def commentField = getFieldById("comment")

if (customField.getValue() != "A") {
commentField.setRequired(true) //if the user selects B or C as a choice, make the comment field required
}
else {
commentField.setRequired(false) //if the user does not select B or C, the comment field will not be required
}

Does the logic here match what you're trying to accomplish?

Andrew L August 16, 2017

Joshua

My requirement is i don't care what choice is selected.  Every value is correct.

The Comment field is mandatory for the user to explain why the custom field value was changed.

 

Thanks

--Andrew

TAGS
AUG Leaders

Atlassian Community Events