Script runner behavior for issue type and custom field

Manoj
Contributor
June 8, 2023

Hello Community,

there are 2 issue types and custom field is finance service with drop down values (ABC,DEF,GHI,JKL,MNO,PQR,STU,VWX, YZ)

Issue type - IRB1

Issue type -IRB2 

now, what i need is - while creating a ticket, user select issue type - IRB1 then in finance service list to appear ABC,DEF,GHI,JKL,MNO) end rest for other issue type IRB2 when user create a ticket.

how to plot this ussing script runner behavior?

1 answer

1 accepted

0 votes
Answer accepted
Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 8, 2023

Hi @Manoj

For your requirement, you will need to use ScriptRunner's Behaviour Initialiser.

Below is a sample working code for your reference:-

import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

@BaseScript FieldBehaviours behaviours
def sampleList = getFieldByName('Sample List')
def project = issueContext.projectObject
def options = []

if (project.key == 'MOCK') {
options = ['Option 1', 'Option 2']
} else if (project.key == 'ST') {
options = ['Option 3', 'Option 4']
}
sampleList.setFieldOptions(options)

Please note that the sample code above is not 100% exact to your environment. Hence, you will need to make the required modifications.

Below is a screenshot of the Behaviour configuration:-

behaviour_config.png

If you observe the screenshot above, the Behaviour is configured for two projects, i.e. Mock and Sample Test.

In the Initialiser section, the conditions are added to determine what value will be set to the list's option when the project is changed.

So in this example, if the project is MOCK, the list will display options Option 1 and Option 2, and if the project is Sample Test (ST), the list will display options Option 3 and Option 4. 

Below are a couple of test screenshots for your reference:-

1) If the project is MOCK, the Sample List will be filtered to Option 1 and Option as shown below:-

test1.png

2) If the Project is Service Test, the sample List will be filtered to Option 3 and Option 4, as shown below:-

test2.png

I hope this help to solve your question. :-) 

Thank you and Kind regards,
Ram

Manoj
Contributor
June 9, 2023

@Ram Kumar Aravindakshan _Adaptavist_ 

 Hello Ram, thanks a lot for investing your efforts and replying back to me on query. 

as you have mentioned above - So in this example, if the project is MOCK, the list will display options Option 1 and Option 2, and if the project is Sample Test (ST), the list will display options Option 3 and Option 4. 

however i have 2 issuetypes in my project and custom field finance service. 

I have just tried the code replacing the fields in my jira environment, though it is not throwing error but it is not working at all.

 

============================================the script i am trying to run is below:

import jsp.secure.admin.views.issuetypes.issuetypeschemes_jsp
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

@BaseScript FieldBehaviours behaviours
def financeservice = getFieldByName('financeservice')
def project = issueContext.projectObject
def options = []

if (project.key == 'IRB1') {
 options = ['Application Development', 'Application Testing']
} else if (project.key == 'IRB2') {
 options = ['Application Development onshore', 'Application Testing onshore']
}
financeservice.setFieldOptions(options)
it is not showing any error but this does not restrict the finance service (custom field) with long list. 
May i request you to guide what is wrong here.
Thanks
M
Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 9, 2023

Hi @Manoj

If you want to do it with different issue types instead of different projects, the approach is pretty much the same.

You will need to modify the code to something like:-

import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

@BaseScript FieldBehaviours behaviours
def sampleList = getFieldByName('Sample List')
def issueType = issueContext.issueType.name
def options = []

if (issueType == 'Bug') {
options = ['Option 1', 'Option 2']
} else if (issueType == 'Story') {
options = ['Option 3', 'Option 4']
}
sampleList.setFieldOptions(options)

Please note that the sample working code above is not 100% exact to your environment. Hence, you will need to make the required modifications.

After going through your code, I have one question, are you sure that the name of the Single Select List is financeservice, i.e. all in lowercase without any spacing?

Could you please share a screenshot of the field from the Custom Fields section?

Below is an updated screenshot of the configuration for your reference:-

behaviour_config.png

Below are a couple of test screenshots for your reference:-

1) If the issue type is a Bug, only Option 1 and Option 2 are displayed.

bug_type.png

 

2) If the issue type is Story only Option 3 and Option 4 are displayed.story_type.png

I hope this helps to answer your question. :-)

Thank you and Kind regards,

Ram

Manoj
Contributor
June 9, 2023

@Ram Kumar Aravindakshan _Adaptavist_ 

Hi, sharing below screen shot for reference, I did check on finance service, it has space between finance & service and i have maintain the same while writing the script.

pls see the screnshot and 1 important thing these 2 issue has different workflow. 

Do i need write separate code for the same.? kindly suggest. Test behavior.jpg

Manoj
Contributor
June 9, 2023

@Ram Kumar Aravindakshan _Adaptavist_ Hello again, 

i have observe the script shared by you again, carefully indeed and got to know what i was missing. 

def financeService = getFieldByName('Finance Service'
i was keeping finance service same everywhere while writing hence it was not working.
Thanks alot again @Ram Kumar Aravindakshan _Adaptavist_  you made my weekend. now i can work further on this project as major one achieved. 
Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 9, 2023

Hi @Manoj 

Great to hear the solution worked for you. 😀

Please accept the answer.

Thank you and Kind regards,

Ram

Manoj
Contributor
June 9, 2023

@Ram Kumar Aravindakshan _Adaptavist_  Hello Gm,

yes I accepted the answer.

import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

@BaseScript FieldBehaviours behaviours

I am unable to understand above script lines, is this something like we have class or library in Atlassian which keeps recorded script and from there we pull the relative one and use.

Could you pls help to know about this.

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 13, 2023

Hi @Manoj

So the BaseScript class is a standard annotation from the Groovy API. Where as the FieldBehaviour class is from ScriptRunner. It is used to be able to access the Behaviour methods from any editor.

By default, when you use the Inline Behaviour editor, this is not required as these functions are already inbuilt. However, if you use ScriptRunner's Editor, you will need to include this to be able to invoke the Behaviour methods.

Thank you and Kind regards,

Ram

Like Manoj likes this
Manoj
Contributor
June 16, 2023

@Ram Kumar Aravindakshan _Adaptavist_ Thanks, for explaining.

Manoj
Contributor
June 18, 2023

@Ram Kumar Aravindakshan _Adaptavist_ Hi, Now i am trying to create similar process for custom field.

custom field is Pillar and another custom field is Platform, so based on pillar values only values relevant to that pillar will appear and platform. 

Instead of issue type i want to use custom field.

def issueType = issueContext.issueType.name
Manoj
Contributor
June 19, 2023

@Ram Kumar Aravindakshan _Adaptavist_ Hi, GM, can you pls suggest.

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 19, 2023

Hi @Manoj

If you want to take a value from a custom field, you cannot use the Initialiser anymore.

You will need to create a Server-Side Behaviour for that field, and your Server-Side Behaviour code will be something like this:-

import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

@@BaseScriptFieldBehaviours behaviours
def sampleList = getFieldById(fieldChanged)
def sampleListValue = sampleList.value as String
def sampleText = getFieldByName('Sample Text')


if (sampleListValue == 'Option 1') {
sampleText.setFormValue('Test 1')

} else if (sampleListValue = 'Option 2') {
sampleText.setFormValue('Test 2') 
}

Please note that the sample code above is not 100% exact to your environment. Hence, you will need to make the required modifications.

For more information on configuring a Server-Side Behaviour, I suggest going through this ScriptRunner Documentation.

I hope this helps to answer your question. :-)

Thank you and Kind regards,

Ram

Manoj
Contributor
June 20, 2023

@Ram Kumar Aravindakshan _Adaptavist_ Thanks, so samplelist in your code would be Pillar or platform? I tried replacing fields name but no luck. 

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 20, 2023

Hi @Manoj

What type of fields are Pillar and Platform?

Thank you and Kind regards,

Ram

Manoj
Contributor
June 20, 2023

@Ram Kumar Aravindakshan _Adaptavist_  These are custom fields DropDown Values

Pillar has 5 Values - Such department like HR, Marketing, IT, Sales and Others.

Platforms has more than 20 values such sub-departments as HR_Recruitmen, HR_TA etc etc.

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 20, 2023

Hi @Manoj 

Are they both single select lists?

Thank you and Kind regards,

Ram

Manoj
Contributor
June 20, 2023
Manoj
Contributor
June 20, 2023

@Ram Kumar Aravindakshan _Adaptavist_ To me more specific , what i want to achieve is when I select HR Department in Pillar Field (custom field)  and then it only shows the sub-department in platforms custom field. 

if (Pillar == 'HR Department') {
 options = ['HR_Recruitment','HR-L&D','HR-Admin']
} else if (Pillar == 'Marketing') {
 options = [''Digital Marketing', 'Rural-Marketing','Urban-Marketing']
Manoj
Contributor
June 21, 2023

@Ram Kumar Aravindakshan _Adaptavist_ Hi, GM ,

Any suggestion pls.

Thanks

M

Manoj
Contributor
July 4, 2023

@Ram Kumar Aravindakshan _Adaptavist_ hello, could you pls suggest.

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 4, 2023

@Manoj

From your latest comment, it appears that you want to use one list to filter the options of a second list field. Is this correct? If yes, then this is a pretty common question.

There are a couple of Community questions already answered for this. For example, this Community Post 

Below is an example working code for your reference:-

import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

@BaseScript FieldBehaviours behaviours

def products = getFieldById(fieldChanged)
def productsValue = products.value.toString()

def available= getFieldByName("Available Options")

def customFieldManager = ComponentAccessor.customFieldManager
def optionManager = ComponentAccessor.optionsManager
def list1CustomField = customFieldManager.getCustomFieldObject(available.fieldId)

def availableConfig = list1CustomField.getRelevantConfig(issueContext)
def list1Options = optionManager.getOptions(availableConfig)

def availableOptions = []

if(productsValue == "Samsung") {
availableOptions = ["TV","Mobile Phone", "Charger", "Laptop", "Memory Cards", "SSD", "Tablet"]
} else if(productsValue in ["Sony","Apple"] ) {
availableOptions = ["TV","Mobile Phone", "Charger", "Laptop"]
}

def availableOptionsMap = list1Options?.findAll {
it.value in availableOptions
}?.collectEntries {
[ (it.optionId.toString()) : it.value ]
}

available.setFieldOptions(availableOptionsMap)

I hope this helps to answer your question. :-)

Thank you and Kind regards,

Ram 

Manoj
Contributor
July 6, 2023

@Ram Kumar Aravindakshan _Adaptavist_  Thanks for replying back, yes its correct "one list to filter the options of a second list field". 

should i replace products (in your script) with pillar (in my script) as per my requirement? 

Here I have pillar and P&S platforms, so basis on value selected in pillar, P&S platforms show the list of values relevant to value/text selected in pillar.

kindly suggest.

 

Thanks!

M

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events