Forums

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

Script for comparing recipient and participent

Jason Li Ting Chung
Contributor
February 7, 2021

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

0 votes
mogavenasan
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.
February 9, 2021

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
AUG Leaders

Atlassian Community Events