Snippets

Adaptavist Set User Picker Field Based On Value Entered in Description In Customer Portal

Created by Kristian Walker last modified
/*
* This example script listener script should be conifgured to run on the issue created event for a Jira Service Management project, in order to set a user in a user picker field who was specified by name in the issue description. 
* "All right, title and interest in this code snippet shall remain the exclusive intellectual property of Adaptavist Group Ltd and its affiliates. Customers with a valid ScriptRunner
* license shall be granted a  non-exclusive, non-transferable, freely revocable right to use this code snippet only within their own instance of Atlassian products. This licensing notice cannot be removed or
* amended and must be included in any circumstances where the code snippet is shared by You or a third party." 
*/

import java.util.regex.Matcher;
import java.util.regex.Pattern;

// Get  all custom fields
def customFields = get("/rest/api/3/field")
        .asObject(List)
        .body
        .findAll { (it as Map).custom } as List<Map>

// Get the ID for your user picker field field
def userPickerField = customFields.find { it.name == 'Demo User Picker' }?.id // Change to name of your field here

// Get the current issue summary and todays date
def descriptionVal = issue.fields.description

// Define the regular expression pattern used to find the user in the description text
def regex = '(?<=The owner of this ticket is:).*';
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(descriptionVal);

// Extract the name of the user after the text The owner of this ticket is:
if(matcher.find()){
def user = matcher.group(0)

// Find the user details for the user matched from the description
def userSearchRequest = get("/rest/api/3/user/search")
        .queryString("query", user)
        .asObject(List)

// Assert that the API call to the  user search API returned a success response
assert userSearchRequest.status == 200

// Get the accountId for the user returned by the search
def userAccountId = userSearchRequest.body.accountId[0]

// Update the issue to set the user picker field to the returned user
def setUserPickerField = put('/rest/api/3/issue/' + issue.key)
        .header('Content-Type', 'application/json')
        .body([
                fields: [
                        (userPickerField): [id: userAccountId],
                ]
        ])
        .asString()
}

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.