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

set field based on part of another field

Jenifer Kuntz February 9, 2015

Using JIRA Service Desk, customers are set with their email address as their user ID.  Based on the domain in their email I need to set a custom field (client)

I have Script runner and think that will do what I need but not sure where to start.

Any help would be appreciated.

 

1 answer

1 accepted

2 votes
Answer accepted
Alejo Villarrubia [Adaptavist]
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 10, 2015

Hi Jenifer,

You can build a custom script and add it to the Create transition of the workflow used in that Service Desk. That post function should be called right after the issue is created.

I believe the following code will do more or less what you need:

import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.bc.issue.IssueService.UpdateValidationResult
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueInputParameters

def Issue issue = issue

// retrieve user
def currentUser = ComponentManager.getInstance().jiraAuthenticationContext?.user

// update issue
if (currentUser?.emailAddress) {
    def IssueService issueService = ComponentAccessor.issueService
    def User adminUser = ComponentAccessor.userUtil.getUser("admin")
    def mailDomain = currentUser.emailAddress.substring(currentUser.emailAddress.indexOf("@")+1)

    IssueInputParameters issueInputParameters = issueService.newIssueInputParameters();
    issueInputParameters.addCustomFieldValue(10704, mailDomain)

    UpdateValidationResult validationResult = issueService.validateUpdate(adminUser, issue.id, issueInputParameters)
    if (validationResult.valid) {
        issueService.update(adminUser, validationResult)
    } else {
        log.debug(validationResult.errorCollection)
    }
}
Jenifer Kuntz February 10, 2015

Thank you so much! It worked perfectly!

Jenifer Kuntz February 10, 2015

I tested this on a regular test project and worked perfect. Added the custom field and the script to the Service Desk project and it is not working. Same user in both instances. I am assuming it has something to do with Service Desk seeing the user as a customer??? Any thoughts? Thank you again I really appreciate it! Jenifer

Alejo Villarrubia [Adaptavist]
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 11, 2015

Yeah, it could definitely be related to the way users are handled in Service Desk. It might be worth to change def currentUser = ComponentManager.getInstance().jiraAuthenticationContext?.user into def currentUser = ComponentAccessor.jiraAuthenticationContext?.user as the first call is actually deprecated. If that does not help, I would suggest to add some logging information around the retrieval of the user.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events