set field based on part of another field

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

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)
    }
}

Thank you so much! It worked perfectly!

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
Community showcase
Asked Thursday in Jira Service Desk

Looking for teams who switched from email to Jira Service Desk

The Jira Service Desk marketing team is working on a guide to help new Atlassian customers switch from email to JSD and we'd love to hear from you! Please share: - What made you realize that i...

201 views 1 5
View question

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you