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

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

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 for comparing recipient and participent

Hi Community,

I have written a script on mailhandler to perform the following functionlality - compare recipients and participents of an exisiting tickets. If it is true, create a new ticket with the recipients as participants. Find below part of my script:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.exception.CreateException
import com.atlassian.jira.service.util.handler.MessageHandlerContext
import com.atlassian.jira.service.util.handler.MessageUserProcessor
import com.atlassian.jira.service.util.ServiceUtils
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.user.util.UserManager
import com.atlassian.mail.MailUtils
import com.atlassian.jira.issue.comments.CommentManager
import com.opensymphony.util.TextUtils

import javax.mail.Address
import javax.mail.Message
import javax.mail.internet.InternetAddress

final targetProjectKey = 'TE'

def messageUserProcessor = ComponentAccessor.getComponent(MessageUserProcessor) as MessageUserProcessor
def userManager = ComponentAccessor.getComponent(UserManager)
def projectManager = ComponentAccessor.projectManager
def issueFactory = ComponentAccessor.issueFactory
def customFieldManager = ComponentAccessor.customFieldManager
def subject = message.getSubject() as String
def body = MailUtils.getBody(message) as String
ApplicationUser user = userManager.getUserByName("jason")
ApplicationUser reportermail = messageUserProcessor.getAuthorFromSender(message) ?: user
def issueFromSubject = ServiceUtils.findIssueObjectInString(subject)
def commentManager = ComponentAccessor.getCommentManager()
def project = projectManager.getProjectObjByKey(targetProjectKey)

if(issueFromSubject){
def cf = ComponentAccessor.customFieldManager.getCustomFieldObjectsByName("Request participants")[0]
def cfVal = issueFromSubject.getCustomFieldValue (cf)?.collect

{ApplicationUser userNow -> user?.getUsername()}

def addressesTO = message.getRecipients(Message.RecipientType.TO) as List<Address>
def addressesCC = message.getRecipients(Message.RecipientType.CC) as List<Address>
def addressesBCC = message.getRecipients(Message.RecipientType.TO) as List<Address>
List<Address> addresses = []

if (addressesTO)

{ addresses.addAll(addressesTO) }

if (addressesCC)

{ addresses.addAll(addressesCC) }

if (addressesBCC)

{ addresses.addAll(addressesBCC) }

addresses.each{
InternetAddress internetAddress = it as InternetAddress

def email = internetAddress.address
def fullName = internetAddress.getPersonal() ?: email
def mailparticipant = userManager.getUserByName(fullName)

cfVal.each{
if(!fullName || !mailparticipant){

def issueObject = issueFactory.getIssue()
issueObject.setProjectObject(project)
issueObject.setSummary(subject)
issueObject.setDescription(MailUtils.getBody(message))
issueObject.setIssueTypeId(project.issueTypes.find

{ it.name == "request participants"}

.id)
issueObject.setReporter(reportermail)

messageHandlerContext.createIssue(user, issueObject)
}
}
}
}

The problem is that when the result is true, nothing happen.

Can you provide support on the issue please.

Thank you advance for your great help and support.

Best regards,
Jason Li

1 answer

Hi @Jason Li Ting Chung,

Just to be on the safe side, you might want to try this:

cfVal.each {
if(!fullName || !mailparticipant) {
// log something out first
}
}

This is to make sure the script is executing the codes within the if that we have.

Next, to create an issue using Scriptrunner, we can refer to this example. It should be something like this in your case: 

cfVal.each {
if(!fullName || !mailparticipant) {
def issueInputParameters = issueService.newIssueInputParameters().with {
    setProjectId(project.id)
    setIssueTypeId(issueType.id)
    setReporterId(reporter.key)
    setSummary(summary)
}

def validationResult = issueService.validateCreate(loggedInUser, issueInputParameters)
assert validationResult.isValid() : validationResult.errorCollection

def result = issueService.create(loggedInUser, validationResult)
assert result.isValid() : result.errorCollection
}
}

You want to make sure the values that we are setting for the Project ID, Issue type ID and etc are correct before passing them to issueInputParameters varaible.

I hope that this helps.

Thanks,
Moga

Suggest an answer

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

Why upgrade to Jira Service Management Premium?

We often have questions from folks using Jira Service Management about the benefits to using Premium. Check out this video to learn how you can unlock even more value in our Premium plan.  &nb...

235 views 1 6
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