Forums

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

Custom field all ways needs to show the last priority field updated info

Inetinfo Inetinfo November 14, 2023

Senari

1) We have custom field {text type} and custome field name is "change field info" and same custom filed id is"custome_  23490"

Senario:

Custom field all ways needs to show the value of before updated value of "priority " filed.

Example:

Priority filed have: Low, high, medium 

We changed priority field for a incident

From "Low" to "High"

Inthis case custome filed needs to show "Low" ( that means previous value of Priority Filed)

Same thing work for N number of times priority filed change.

Note: if it is no update for priority field then custome filled should be blank empty

Please let me know best approach and jira code for senario 

 

 

1 answer

0 votes
Murat Seven
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 14, 2023

Hi @Inetinfo Inetinfo  and thanks for your question.

 

For which Jira environment are you asking (Cloud- Server/DataCenter)?

 

I think, automation or Scriptrunner would be needed for this. For example, if you are using Scriptrunner you can use a listener script in Scriptrunner to capture the "changelog" information and, based on the changes in the "priority" field, update the custom field with its previous value.

Hope this helps. If the issue is resolved, you can vote and accepted for this comment.

 

Best,

Murat Seven

 

 

 

 

Inetinfo Inetinfo November 14, 2023

Thanks @Murat Seven 

Yes it's script runner

Can you expand the steps and code

Murat Seven
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 14, 2023

Hi @Inetinfo Inetinfo 

 

For which Jira environment?

For Jira Cloud;

 

You can use Script Listeners for this purpose. Choose the "Issue Updated" events for these listeners.

Afterward, you can make a REST API request with this script and write the result to the desired field.

 

def changelogResponse = get("rest/api/2/issue/${issue.key}/changelog")
.header('Content-Type', 'application/json')
.asObject(Map).body

def priorityChanges = changelogResponse.values.items.findAll{it['field'].getAt(-1) == 'priority'}

logger.info("priorityChanges: "+ priorityChanges)
def previousPriority = priorityChanges.getAt(-1).getAt(-1).fromString

// Update the issue with the PreviousPriority field
put("/rest/api/2/issue/${issue.key}")
.header('Content-Type', 'application/json')
.body([
fields: [
customfield_10071: previousPriority.toString() // customfield ID for PreviousPriority is customfield_10071
]
])
.asString()

Hope this helps.

 

Best,

Murat Seven

Inetinfo Inetinfo November 15, 2023

Thank you @Murat Seven Murat Seven

getting following error 

2023-11-16 05:55:30,147 ERROR [runner.AbstractScriptListener]: *************************************************************************************
2023-11-16 05:55:30,148 ERROR [runner.AbstractScriptListener]: Script function failed on event: com.atlassian.jira.event.issue.IssueEvent, file: null
groovy.lang.MissingMethodException: No signature of method: org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.get() is applicable for argument types: (org.codehaus.groovy.runtime.GStringImpl) values: [rest/api/2/issue/Dcco-5675/changelog]
Possible solutions: get(java.lang.String), grep(), grep(java.lang.Object), getAt(java.lang.String), wait(), any()
at Script3018.run(Script3018.groovy:119)

......................................

code implemented on listener 

// the value of the new option to set

def log = Logger.getLogger("setResolutionDate")
log.setLevel(Level.WARN)
//
// Get Managers and Components
def issueManager = ComponentAccessor.getIssueManager()
def userManager = ComponentAccessor.getUserManager()
def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)
def SearchService = ComponentAccessor.getComponent(SearchService)
def issueIndexService = ComponentAccessor.getComponent(IssueIndexingService.class)
def changeHistoryManager = ComponentAccessor.getChangeHistoryManager()

def customFieldManager = ComponentAccessor.getCustomFieldManager()
MutableIssue issue = event.getIssue()

def commentManager = ComponentAccessor.getCommentManager()
def changelogResponse = get("rest/api/2/issue/${issue.key}/changelog")
.header('Content-Type', 'application/json')
.asObject(Map).body

def priorityChanges = changelogResponse.values.items.findAll{it['field'].getAt(-1) == 'priority'}

logger.info("priorityChanges: "+ priorityChanges)
def previousPriority = priorityChanges.getAt(-1).getAt(-1).fromString

// Update the issue with the PreviousPriority field
put("/rest/api/2/issue/${issue.key}")
.header('Content-Type', 'application/json')
.body([
fields: [
customfield_26800: previousPriority.toString()
]
])
.asString()

 

........................

Suggest an answer

Log in or Sign up to answer