Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

groovy script to update a custom field value depending on a the reporter group

wajih zouaoui
Contributor
July 11, 2019

Hi,

 

i'm trying to write a post-function script in order to update a custom value field depending a user reporte's group.

If the user belongs to "group1" then the field will have the value " value1"

 

Here is the script i've tried to write and can you please help me to make a correct version of it ?

 

 

I'm using Scriptrunner 5.5.7

 

 

Thanks in advance.

 

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


def customFieldManager = ComponentAccessor.customFieldManager
def SDG = customFieldManager.getCustomFieldObjectsByName("Société de gestion (SDG)").first()
def groupManager = ComponentAccessor.getGroupManager()


def optionsManager = ComponentAccessor.getOptionsManager()
def config = SDG.getRelevantConfig(issue)
def options = optionsManager.getOptions(config)




if(groupManager.isUserInGroup(issue.reporter?.contains("XXX"))
{

def optionToSelect = options.find {it.value == "XXX"}
SDG.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(SDG),optionToSelect))
}

 

1 answer

1 accepted

0 votes
Answer accepted
Mark Markov
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.
July 11, 2019

Hello @wajih zouaoui 

try this

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption

def customFieldManager = ComponentAccessor.customFieldManager
def SDG = customFieldManager.getCustomFieldObjectsByName("Société de gestion (SDG)").first()
def groupManager = ComponentAccessor.getGroupManager()
def issueManager = ComponentAccessor.getIssueManager()


def optionsManager = ComponentAccessor.getOptionsManager()
def config = SDG.getRelevantConfig(issue)
def options = optionsManager.getOptions(config)




if (groupManager.isUserInGroup(issue.reporter, "group1")) {
def optionToSelect = options.find { it.value == "value1" }
issue.setCustomFieldValue(SDG, optionToSelect)
issueManager.updateIssue(issue.reporter, issue, EventDispatchOption.DO_NOT_DISPATCH, false)
}
wajih zouaoui
Contributor
July 11, 2019

Hello Mark,

 

it works great and i want to thank you for your help.

 

Have a nice day.

 

Best Regards,

Wajih

wajih zouaoui
Contributor
July 11, 2019

Hello again @Mark Markov ,

 

can we please create a script that it add automatically the right value on the custom field depending of the user group ?

 

It's like a behavior who try to find if there is a match with one group and a value from the custom field and then it choose the right value to fill on the custom field.

The custom field is not shown for users and it must be filled by the behavior script for view screen only.

 

FYI, every value on the field has the same as a user group.

 

cf value 1 = grp value1 

..

 

 

Thanks a lot .

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events