ScriptRunner: How to update a custom field if Description changed? Edited

Goal: identify all stories where Description field changed after certain date.

My thinking:

  • Create a custom field (number) and update counter each time Description is updated
  • Have ScriptRunner update the custom field on each Description update event
  • Use JQL and generate report (the easy part)

STEP 1: Created custom field

  "id": "customfield_16800",
    "name": "DescriptionChange",
    "custom": true,
    "orderable": true,
    "navigable": true,
    "searchable": true,
    "clauseNames": [
      "cf[16800]",
      "DescriptionChange"
    ],
    "schema": {
      "type": "number",
      "custom": "com.atlassian.jira.plugin.system.customfieldtypes:float",
      "customId": 16800
    }

STEP 2: created Custom Listener (All Projects, "All Issue Events")

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder

Issue issue = event.issue
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def change = event?.getChangeLog()?.getRelated("ChildChangeItem").find {it.field == "Description"}

if (change)
{
    def tgtField = customFieldManager.getCustomFieldObjects(issue).find {it.name == "DescriptionChange"}
    def changeHolder = new DefaultIssueChangeHolder()
    tgtField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(tgtField), 1), changeHolder)
}
else return

 

The script doesn not update Description field.  I am new to ScriptRunner and need some help with the script.

Thanks in advance!

1 answer

1 vote

You could do this entirely with a scripted field. This is a variation of this example in the docs here.

You'll need the following script below:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueFieldConstants

def changeHistoryManager = ComponentAccessor.getChangeHistoryManager()

def changeItems = changeHistoryManager.getChangeItemsForField(issue, IssueFieldConstants.DESCRIPTION)

if (changeItems) {
def changeItem = changeItems.last()

return new Date(changeItem.getCreated().time)
}

null

 You'll need to set your template and searcher to:

Template: Date Time picker
Searcher: Date Time Range picker

Then it should be just a case of crafting the right JQL to search on that field.

For your information ScriptRunner provides a number of JQL functions for comparing dates here. If you happen to have a more complex requirement in the future.

Hope this helps.

Hi Adam, thank you for the feedback.  During implementation of this functionality our requirement got refined into the following: Only track Description field changes during Active Sprint.  Should not track any changes before or after Active Sprint.

Here's the code for that:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import org.ofbiz.core.entity.GenericValue
import com.atlassian.core.util.map.EasyMap

def changeHistoryManager = ComponentAccessor.getChangeHistoryManager()
def event = (IssueEvent)event
def issue = event.issue
def customFieldManager = ComponentAccessor.getCustomFieldManager()

def sprintField = customFieldManager.getCustomFieldObjects(issue).find {it.name == "Sprint"}
if (sprintField)
{
    def state = issue.getCustomFieldValue(sprintField)?.state?.first().toString()
    if (state == "ACTIVE")
    {
        List<GenericValue> changeItems = null;
        try {
            GenericValue changeLog = event.getChangeLog()
            Map map = EasyMap.build("group", changeLog.get("id"))    
            changeItems = changeLog.getDelegator().findByAnd("ChangeItem", map)
            if(changeItems == null) {
                log.warn("nothing found")
            }
            for(GenericValue changeVal : changeItems) {
                String field = changeVal.getString("field")

                if (field == "description") {
                    log.warn("Detected change to description field")
                    def tgtField = customFieldManager.getCustomFieldObjects(issue).find {it.name == "DescriptionChange"}
                    log.warn("tgtField " + tgtField)
                    def changeHolder = new DefaultIssueChangeHolder()
                    Double num = (Double)(issue.getCustomFieldValue(tgtField))
                    if(num == null) {
                        num = 0;
                    }
                    log.warn("num " + num)
                    tgtField.updateValue(null, issue, new ModifiedValue(num, num + 1), changeHolder)
                }
            }
        } catch(Exception e) {

        }
    }
    else {return}
}
else {return}

 

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Monday in Jira Software

How large do you think Jira Software can grow?

Hi Atlassian Community! My name is Shana, and I’m on the Jira Software team. One of the many reasons this Community exists is to connect you to others on similar product journeys or with comparabl...

711 views 6 13
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