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

Restrict custom field values in Request Type groups in customer portal using Behaviour Edited

Hello All,

We have Organized Jira Service desk Request Types to multiple Groups to appear in the customer portal Ex: Jira, Confluence, Bitbucket and Bamboo.

Each Group has multiple Request types For Example: Jira has following Request types New Account, modify Field, Edit Project Reporting and Bug.
Group confluence has following Request types New Confluence Space, Additional functionality, Bug and Performance

For Groups Jira and confluence has following fields on Customer portal Summary, Description, Attachment and Servers. Servers custom field has following Options Server1, Server2, Server3 and Server4.

Now we want to Restrict the values on the Servers field depend on the Request Type group. Ex - If user selects a Group Jira and Request name Bug on the customer portal Servers field
has to show following options Server1 and Server2 and same way if user selects Confluence group and Request name Bug Servers field has to show only following options Server 2 and Server 3.

To achieve this we have added below script in the behavior and mapped to Jira service desk project and All Request Types. But Servers custom field is showing all 3 options in Groups Jira , Confluence, BitBucket and Bamboo in Bug request type which we added in the script.


import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.customfields.option.Option

def issueField = getFieldByName("Servers")

// Get the current user
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def optionsManager = ComponentAccessor.getOptionsManager()

def customField = customFieldManager.getCustomFieldObject(issueField.fieldId)
def config = customField.getRelevantConfig(getIssueContext())
// Get predifined customField options
def options = optionsManager.getOptions(config)

def groupProjectsMap = [
'Jira': ['Server1','Server2'],
'Confluence': ['Server3'],

]
def optionsToSet = []

log.debug("Map: ${groupProjectsMap}")

for (r in groupProjectsMap) {
def option = options.findAll {it.value in r.value} as List<Option>
optionsToSet.add(option)
}

log.debug("Options: ${optionsToSet.flatten()}")
issueField.setFieldOptions(optionsToSet.flatten())

Please help!

Thanks in advance,

Ch

1 answer

1 accepted

0 votes
Answer accepted
brbojorque Community Leader Dec 22, 2019

First of all this lines below does the make much sense. 

for (r in groupProjectsMap) {
def option = options.findAll {it.value in r.value} as List<Option>
optionsToSet.add(option)
}

What you need to do find out what Request type is selected and do and if else code block

if(requestType.value == "Jira"){
def option = ['Server1', 'Server2']
}else if(requestType.value == 'Confluence'){
def option = ['Server2', 'Server3']
}

optionsToSet.add(option)
issueField.setFieldOptions(optionsToSet.flatten())

Let me know if it works for you. 

Hi @brbojorque

As your suggestion modified the script. It showing below error.

Error - The variable [requestType] is undeclared.

Below is the script which i modified:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.customfields.option.Option

def issueField = getFieldByName("Servers")

// Get the current user
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def optionsManager = ComponentAccessor.getOptionsManager()

def customField = customFieldManager.getCustomFieldObject(issueField.fieldId)
def config = customField.getRelevantConfig(getIssueContext())
// Get predifined customField options
def options = optionsManager.getOptions(config)

def groupProjectsMap = [
'Jira': ['Server1','Server2'],
'Confluence': ['Server3'],

]
def optionsToSet = []

log.debug("Map: ${groupProjectsMap}")

if(requestType.value == "Jira"){
def option = ['Server1', 'Server2']
}else if(requestType.value == 'Confluence'){
def option = ['Server2', 'Server3']
}

optionsToSet.add(Option)
issueField.setFieldOptions(optionsToSet.flatten())

 

Attaching screen shots for your reference:

Screen Shot 2019-12-22 at 8.11.56 PM.pngScreen Shot 2019-12-22 at 9.40.16 PM.pngScreen Shot 2019-12-22 at 9.41.11 PM.pngScreen Shot 2019-12-22 at 9.41.20 PM.pngScreen Shot 2019-12-22 at 9.41.50 PM.png

 

Please let me know if you need any further details.

Thanks in advance,

Manikanta

Hi @Ch ,

Please check the solution, I tried to scour the internet in order for me to get the current request type used in the service desk and I can't for the life of me get the solution.

Here's the workaround, it will determine the group by checking the issuetype of the current request.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.customfields.option.Option
def issueField = getFieldByName("Server")


// Get the current issue type
def issue = getIssueContext()

// Get the current user
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def optionsManager = ComponentAccessor.getOptionsManager()

def customField = customFieldManager.getCustomFieldObject(issueField.fieldId)
def config = customField.getRelevantConfig(getIssueContext())

def options = [:]
def listOpt = []

if(issue.getIssueType().name == 'Service Request'){ // IssueTypes inside Confluence Group
listOpt = ["Server 1", "Server 2"]
}else if (issue.getIssueType().name == 'IT Help'){ //IssueTypes inside Jira Group
listOpt = ["Server 3", "Server 4"]
}

options = optionsManager.getOptions(config).findAll{ it.value in listOpt }
issueField.setFieldOptions(options)

 Caveat

If you have multiple Issuetype mixed in both groups this will not work, it must be that the issuetype is unique to each group.

Thanks for taking time and helping me. Will it be possible to get Request type Groups in Script Runner behavior?

brbojorque Community Leader Dec 23, 2019

Hi @Ch ,

By using RequestTypeService and RequestTypeGroups clases, it is easy to fetch the list of groups and request types.

The problem is getting the Request type of the current Context, without that piece of information the list of request types and groups does not really matter.

I'd have to do further research for this.

Like Ch likes this

Thanks for the help again @brbojorque

Please let me know if find any solution. I will also try to find for solution on my end.

Thanks again,

ch

brbojorque Community Leader Dec 30, 2019

Hi @Ch ,

I think I have found the solution in getting the request type.

Please take a look at this example.

https://scriptrunner.adaptavist.com/latest/jira/recipes/workflow/postfunctions/auto-add-reviewer-to-request-SD.html

Thanks for the link, tried with the below script it is not working and it is showing below error.

import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.servicedesk.api.requesttype.RequestTypeService
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import com.opensymphony.workflow.WorkflowContext
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.customfields.option.Option
import com.atlassian.servicedesk.api.requesttype.RequestType
import com.atlassian.jira.user.ApplicationUser


@WithPlugin("com.atlassian.servicedesk")

RequestTypeService requestTypeService = ComponentAccessor.getOSGiComponentInstanceOfType(RequestTypeService)
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
ApplicationUser user = ComponentAccessor.getUserManager().getUserByName(ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser().name)
RequestType getRequestType = requestTypeService.getRequestTypeForIssue(user, underlyingIssue).right().get()

def issueField = getFieldByName("Server")

def customFieldManager = ComponentAccessor.getCustomFieldManager()

String requestTypeName = getRequestType.getName()

def optionsManager = ComponentAccessor.getOptionsManager()

def customField = customFieldManager.getCustomFieldObject(issueField.fieldId)
def config = customField.getRelevantConfig(getIssueContext())

def options = [:]
def listOpt = ["Server1", "Server2"]
def listOpt1 = ["Server3", "Server4"]

if ("Jira".equals(requestTypeName)) {
options = optionsManager.getOptions(config).findAll{ it.value in listOpt }
issueField.setFieldOptions(options)
}
else {
options = optionsManager.getOptions(config).findAll{ it.value in listOpt1 }
issueField.setFieldOptions(options)
}

 

Screen Shot 2020-01-05 at 1.01.08 AM.png

Hi @brbojorque

Could you please help me .

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Marketplace Apps & Integrations

Distribute and track teams' work in a smart way and gain visibility

  Plan and track how teams spend their time and effort may represent a rocket science for many team leaders. Not rarely they need to deal with a considerable amount of variables to “hit the tar...

85 views 0 2
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