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

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Script to pdate the Prioirty automatically whenvere it changed

Hello,

 

I  am looking for Script runner with detailed step which fulfils my requirements

My requirement is whenever we change the priority in jira it displays normally Priority changed from Major to Minor but it does not display the same message in service portal which customer refer. Customer is always blind in service portal when the priority is changed

Hence i am looking or a script runner whenever we change the priority from Major to minor in Jira the comment to be automatically added in Service portal which is visible to customer "The priority for this issue has changed from Major to Minor"

I am aware it works with "Automation for Jira - Server" but i dont have this application and looking for only script runner

Thanks and Regards,

Sharad

1 answer

0 votes
Ravi Sagar Community Leader Nov 16, 2020

Hi @Sharadkumar Bangera 

You can create a listener to check when the priority is changed and then add a comment.

Take a look at this post and this one for similar examples.

Give it a try and post your code here if you need further help.

Ravi

Hi Ravi,

 

Thanks for the reply.

I am not expert in Script runner, i created few minor script in Behavior field but those are super easy. Can you please help me to correct below script

I m not sure in the below script where do i define

1) Project should be applicable to only  "TGE" project

2) Comment to be added in Jira whenever someone  change the priority to Critical to Major or Major to Minor and it should display in text ""Priority is changed from Critical to Major" and vice versa

3) Please confirm where do i write the script

Browse
Console
Built-in Scripts
Jobs
Listeners
Fields
JQL Functions
REST Endpoints
Script Editor

------------------------------------

import com.atlassian.jira.bc.issue.comment.CommentService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.servicedesk.api.comment.ServiceDeskCommentService
import com.onresolve.scriptrunner.runner.customisers.WithPlugin

@WithPlugin("com.atlassian.servicedesk")

final String Priority = "Blocker" // the key of the issue
final String comment = "Prioirity is changed to Blocker" // the comment you want to add

static void createServiceDeskComment(MutableIssue issueToComment, String text, Boolean internal) {
def user = ComponentAccessor.jiraAuthenticationContext.loggedInUser

if (issueToComment.projectObject.projectTypeKey.key == "service_desk") {
def serviceDeskCommentService = ComponentAccessor.getOSGiComponentInstanceOfType(ServiceDeskCommentService)

def createCommentParameters = serviceDeskCommentService.newCreateBuilder()
.author(user)
.body(text)
.issue(issueToComment)
.publicComment(!internal)
.build()

serviceDeskCommentService.createServiceDeskComment(user, createCommentParameters)
} else {
def commentParameters = CommentService.CommentParameters.builder()
.author(user)
.body(text)
.issue(issueToComment)
.build()

def commentService = ComponentAccessor.getComponent(CommentService)
def validationResult = commentService.validateCommentCreate(user, commentParameters)
commentService.create(user, validationResult, true)
}
}

//Example of function in use
def issueManager = ComponentAccessor.issueManager
def issue = issueManager.getIssueByCurrentKey(issueKey)

createServiceDeskComment(issue, comment, true) //false means public, true means internal

Hello,

 

I am working on  Listener custom Script for my query whenever the Priorities change the Comment to be added in Jira "Priorities changed from Major to Minor".I am not good in scripting.

I m using below script but  it require a correction. Please help me where do i define the priority and comment "Priorities changed from Major to Minor".

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.model.ChangeGroup
import com.atlassian.jira.model.ChangeItem
import com.atlassian.jira.issue.history.ChangeItemBean

def commentManager = ComponentAccessor.getCommentManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()

def issue = event.issue
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def cf = customFieldManager.getCustomFieldObjectByName("SomeDate")
def sd= cf.getValue(issue) as Date
def someDate = sd.format("yyyy-MM-dd")

String comment = "*Automatic comment:* SomeDate was updated to " + someDate

def changeHistoryManager = ComponentAccessor.getChangeHistoryManager()
def change = event?.getChangeLog()?.getRelated("ChildChangeItem")?.find {it.field == "Single User Picker CF"}

if (change) {
log.debug "Value changed from ${change.oldstring} to ${change.newstring}"

}

 

script.PNG

Hello @Ravi Sagar ,

 

I can see my listener script works perfectly fine whenever prioirity changes the comment to be updated in log.info but i m looking for  whenever priority changes the comment to be added in Jira.

Can you please correct my below script to addcomment

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.bc.issue.comment.property.CommentPropertyService
import com.atlassian.jira.issue.priority.Priority
import com.atlassian.jira.issue.comments.Comment
import com.atlassian.jira.issue.Issue
event.issue.issueType.name == 'Incident'

def CustomFieldManager = ComponentAccessor.getCustomFieldManager()
def commentManager = ComponentAccessor.getCommentManager()
def change = event?.getChangeLog()?.getRelated("ChildChangeItem").find
def comment = event.comment?.body
{it.field == "priority"}

if (change)

{commentManager.create("Value changed from ${change.oldstring} to ${change.newstring}")}
//
{log.warn "Priority Changed"}

//{log.warn "Value changed from ${change.oldstring} to ${change.newstring}"}
//else
//
{log.warn "Priority remain same"}

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Jira

Admins, notify your Jira instance of system-wide changes with the new admin announcement banner

Hi All! We’re excited to share the launch of an announcement banner that lets Jira site administrators communicate directly to their users across their  Jira Cloud instance.  ...

561 views 15 18
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you