How to send an email notification to a group on custom field value selection when issue is created??

mani@123 July 8, 2019

Hello All,

How to send an email notification to a group on custom field value selection when issue is created?

Example: We have a custom field called Product(single select) and it has options Part1, Part2, Part3 and we have 3 groups Group 1, Group 2, Group 3. If user selects option Part1 it has to send notification the users in the Group 1 same as Part2 is selected it has to send notifications to Group 2.

I have added "Fire a Issue Created event(script runner)" on custom field value selection in post function as below but how to add a condition to send email to particular group on custom field value selection?

cfValues['Product']?.value == 'Part1'

I am new to scripting. Can any one please help me.

Thanks in Advance,

Mani 

1 answer

1 accepted

0 votes
Answer accepted
Antoine Berry
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 15, 2019

Hi mani@123 ,

My advice would be the following : 

  1. Create a Group Picker custom field (you can add it to the screen(s) of your project so that you can track its value more easily). 
  2. In the notification scheme of your project, map this field to the "Issue Created", so that users in this group will receive a mail.
  3. Add a script post function in the Create Issue transition. Make sure it is placed after the "creates the issue...." post function, but before "Fire an Issue Created event...". Use this script : 
    import com.atlassian.jira.component.ComponentAccessor
    import com.atlassian.jira.issue.ModifiedValue
    import com.atlassian.jira.issue.util.DefaultIssueChangeHolder

    int selectListId = 13500
    def selectList = customFieldManager.getCustomFieldObject(selectListId)
    def selectListValue = issue.getCustomFieldValue(selectList)

    def groups = ["Part1": "Group1",
    "Part2": "Group2",
    "Part3": "Group3"]

    def groupToSendMail = ComponentAccessor.getUserUtil().getGroup(groups[selectListValue?.getValue()])

    int groupCfId = 13900
    def groupCf = customFieldManager.getCustomFieldObject(groupCfId)
    def groupCfValue = issue.getCustomFieldValue(groupCf)

    if (groupToSendMail != groupCfValue){
    groupCf.updateValue(null, issue, new ModifiedValue(groupCfValue, [groupToSendMail]), new DefaultIssueChangeHolder())
    }

    Make sure to replace the ids. If a value is not mapped, the group picker will not have any value.

Antoine

mani@123 July 17, 2019

Hi Antoine,

Thanks for the reply!

It showing an error in the script:

Community.PNG

Thanks in Advance,

Mani

mani@123 July 17, 2019

@Antoine Berry ,

Is it possible to send notification to single user instead of group on custom field value selection?

Antoine Berry
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 18, 2019

Hi mani@123 ,

Do not worry about the static errors, they will resolve at execution time. 

Yes this is possible to send a mail to a single user, change the notification scheme accordingly, the logic is the same.

mani@123 July 18, 2019

Hi @Antoine Berry

Below error in the log file:

2019-07-17 19:22:00,638 ERROR [workflow.ScriptWorkflowFunction]: *************************************************************************************
2019-07-17 19:22:00,640 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: FLW02-28637, actionId: 1, file: <inline script>
groovy.lang.MissingPropertyException: No such property: customFieldManager for class: Script32
 at Script32.run(Script32.groovy

I am new to scripts. Please help! 

Antoine Berry
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 19, 2019

Sorry, I forgot to add the declaration of the customFieldManager... use this : 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder

def customFieldManager = ComponentAccessor.getCustomFieldManager()

int selectListId = 13500
def selectList = customFieldManager.getCustomFieldObject(selectListId)
def selectListValue = issue.getCustomFieldValue(selectList)

def groups = ["Part1": "Group1",
"Part2": "Group2",
"Part3": "Group3"]

def groupToSendMail = ComponentAccessor.getUserUtil().getGroup(groups[selectListValue?.getValue()])

int groupCfId = 13900
def groupCf = customFieldManager.getCustomFieldObject(groupCfId)
def groupCfValue = issue.getCustomFieldValue(groupCf)

if (groupToSendMail != groupCfValue){
groupCf.updateValue(null, issue, new ModifiedValue(groupCfValue, [groupToSendMail]), new DefaultIssueChangeHolder())
}

Antoine

mani@123 July 22, 2019

Hi Antonie,

Created different workflows for each issue type and added above script in the create transition for story workflow. In the notification scheme mapped Group field to the "Issue Created" event. But it is sending notifications on creation of all issue types in the project

There is no error in the logs.

Thanks in Advance,

Manikanta

Antoine Berry
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 23, 2019

Hi mani@123 ,

If you want to send notifications for only one issue type, you should use a custom event. i.e. : 

  1. Create a new custom event (probably with Issue Created template).
  2. Replace the "Issue created" in notification scheme by this event (or update the scheme accordingly).
  3. In the workflow used by this issue type, in the Create transition, replace the "Issue Created" event by this new event.

That way notifications will only be sent for this event.

Let me know if that helped.

Antoine

mani@123 July 25, 2019

Hi Antonia,

Thanks a lot!

It is working now after creating custom event.

Appreciate for your help.

Thanks Again,

Mani

Like Antoine Berry likes this

Suggest an answer

Log in or Sign up to answer