Forums

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

Setting up Scripted Mail Handler

Shrikant Pandurang Mandlik November 17, 2022

Hi Team , There is one requirement from my side where I am stuck -> Via Incoming Mail handler , I want whenever the mail is sent to particular mail id the ticket would be created but in the project workflow the component/s is set as mandatory in validator , so whenever the mail is sent the incoming mail handler would be scripted in order to set the component/s by default as "test" since component is a system field as well an array I am not getting the function to set it. 

Here is my code, could you please help me to set the Reporter who is sending the mail and assignee a particular mail and the component as "TEST"

CODE :

import com.atlassian.jira.user.util.UserManager
import com.atlassian.jira.service.util.handler.MessageUserProcessor
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.user.ApplicationUsers
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.jira.issue.MutableIssue
import java.util.regex.Pattern;
import java.util.regex.Matcher
import com.atlassian.jira.workflow.TransitionOptions

def issueService = ComponentAccessor.getIssueService()
def userManager = ComponentAccessor.getComponent(UserManager)
def projectManager = ComponentAccessor.getProjectManager()
def issueFactory = ComponentAccessor.getIssueFactory()
def messageUserProcessor = ComponentAccessor.getComponent(MessageUserProcessor)
def project = projectManager.getProjectObjByKey("TESTJIRA")

ApplicationUser user = userManager.getUserByName("abcd")

def issueObject = issueFactory.getIssue()


issueObject.setSummary("Test TPO")
issueObject.setDescription("Test Description")
issueObject.setIssueTypeId(project.issueTypes.find { it.name == "Service Request" }.id)

def customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Components")
issueObject.setCustomFieldValue(customField,"TEST")


messageHandlerContext.createIssue(user, issueObject)

2 answers

1 accepted

1 vote
Answer accepted
Tom Lister
Community Champion
November 17, 2022

Hi @Shrikant Pandurang Mandlik 

Component is a system field and not a custom field.

You can handle it using something similar to:

def myComponent = ComponentAccessor.getProjectComponentManager().findByComponentName(issue.project,'MYCOMP')

issue.setComponent([myComponent])
Shrikant Pandurang Mandlik November 18, 2022

Hi @Tom Lister ,

After using this as well it is giving me NullPointerException and WorkflowException can you please correct the below code :

import com.atlassian.jira.user.util.UserManager
import com.atlassian.jira.service.util.handler.MessageUserProcessor
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.user.ApplicationUsers
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.jira.issue.MutableIssue
import java.util.regex.Pattern;
import java.util.regex.Matcher
import com.atlassian.jira.workflow.TransitionOptions

def issueService = ComponentAccessor.getIssueService()
def userManager = ComponentAccessor.getComponent(UserManager)
def projectManager = ComponentAccessor.getProjectManager()
def issueFactory = ComponentAccessor.getIssueFactory()
def messageUserProcessor = ComponentAccessor.getComponent(MessageUserProcessor)
def project = projectManager.getProjectObjByKey("TESTJIRA")
def myComponent = ComponentAccessor.getProjectComponentManager().findByComponentName(project.getId(),"TEST")

ApplicationUser user = userManager.getUserByName("abcd")

def issueObject = issueFactory.getIssue()


issueObject.setSummary("Test TPO")
issueObject.setDescription("Test Description")
issueObject.setIssueTypeId(project.issueTypes.find { it.name == "Service Request" }.id)
issueObject.setComponent([myComponent])


messageHandlerContext.createIssue(user, issueObject)

Tom Lister
Community Champion
November 18, 2022

Hi @Shrikant Pandurang Mandlik 

Does the component TEST exist? If not the call to find it will return null.

You can test for null and either create a new one on skip the setComponent option

it’s a good idea to put log statements when debugging new code.

Also worth checking that the user is being returned and not null

I’ll be offline now until after the weekend

Shrikant Pandurang Mandlik November 21, 2022

Hi @Tom Lister ,

Thank you so much. It is working.

 

Best Regards,

Shrikant Mandlik

1 vote
Vikrant Yadav
Community Champion
November 17, 2022

Hi @Shrikant Pandurang Mandlik  Similar post , kindly check this link :- 

https://community.atlassian.com/t5/Jira-questions/Scriptrunner-mail-handler-not-updating-custom-select-list-and/qaq-p/2005452

First create the issue, then try to update the issue fields. Reporter you can set before creating an issue. 

Thanks

V.Y

Shrikant Pandurang Mandlik November 18, 2022

Hi @Vikrant Yadav ,

I want to set field before creating issue as I could've used post function for that. But I want to distinguish between the issues created by mail and created via Jira.

So can you please suggest me correction for below code as it is giving me NullPointerException and WorkflowException can you please correct the below code :

import com.atlassian.jira.user.util.UserManager
import com.atlassian.jira.service.util.handler.MessageUserProcessor
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.user.ApplicationUsers
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.jira.issue.MutableIssue
import java.util.regex.Pattern;
import java.util.regex.Matcher
import com.atlassian.jira.workflow.TransitionOptions

def issueService = ComponentAccessor.getIssueService()
def userManager = ComponentAccessor.getComponent(UserManager)
def projectManager = ComponentAccessor.getProjectManager()
def issueFactory = ComponentAccessor.getIssueFactory()
def messageUserProcessor = ComponentAccessor.getComponent(MessageUserProcessor)
def project = projectManager.getProjectObjByKey("TESTJIRA")
def myComponent = ComponentAccessor.getProjectComponentManager().findByComponentName(project.getId(),"TEST")

ApplicationUser user = userManager.getUserByName("abcd")

def issueObject = issueFactory.getIssue()


issueObject.setSummary("Test TPO")
issueObject.setDescription("Test Description")
issueObject.setIssueTypeId(project.issueTypes.find { it.name == "Service Request" }.id)
issueObject.setComponent([myComponent])


messageHandlerContext.createIssue(user, issueObject)

Suggest an answer

Log in or Sign up to answer