It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

set field based on part of another field

Jenifer Kuntz Feb 09, 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

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 Feb 10, 2015

Thank you so much! It worked perfectly!

Jenifer Kuntz Feb 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

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
This widget could not be displayed.
This widget could not be displayed.
Community showcase
Published in Jira Service Desk

4 changes to queues and issues you don’t want to miss

Hello Atlassian Community! I'm Tania, a Senior Product Marketing Manager for Jira Service Desk Cloud! I'm excited to announce some exciting improvements coming up for those who work with queues and...

587 views 9 12
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you