Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Deleted user
0 / 0 points
Next:
badges earned

Your Points Tracker
Challenges
Leaderboard
  • Global
  • Feed

Badge for your thoughts?

You're enrolled in our new beta rewards program. Join our group to get the inside scoop and share your feedback.

Join group
Recognition
Give the gift of kudos
You have 0 kudos available to give
Who do you want to recognize?
Why do you want to recognize them?
Kudos
Great job appreciating your peers!
Check back soon to give more kudos.

Past Kudos Given
No kudos given
You haven't given any kudos yet. Share the love above and you'll see it here.

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 required custom field when selecting option in multi select custom fieldlist

Hi everyone,

I want to create behavior that makes a custom field required when the user selects a specific option in the multi select field.

For example : 

"Deployment Platform" - it's a multi select list custom field

"Impact" - Free text custom field  

Scenario:

I want that when a user selects in the "Deployment Platform" the option "Mobile",

The "Impact" custom field will be required.

 

Thanks

2 answers

Hi @Zachi Meyer ,

Continue from the conversation on your reported ticket. You can use this reference from the Adaptavist library.

This is a sample script I tested on my end manage to work:

import com.onresolve.jira.groovy.user.FieldBehaviours
import org.apache.log4j.Logger
import org.apache.log4j.Level
import groovy.transform.BaseScript

@BaseScript FieldBehaviours fieldBehaviours

// Multi Select List field
def multiSelectField = getFieldByName("Multi1")
// Multi Text Line field
def multiLine1 = getFieldByName('TextMulti1')
// Value for a multi-select field will always be a list even if "None" is selected
def multiSelectFieldValue = multiSelectField.value as List
def multiValue = multiSelectFieldValue.equals("A")
log.warn "=================================> $multiSelectFieldValue"
log.warn "=================================> $multiValue"
def description
// If value is null
if (multiSelectFieldValue == [null]) {
description = 'Multi Select Field is set to None'

// If a given string is selected
} else if (multiSelectFieldValue == ["A"]) {
description = 'Multi Select Field is set to A'
multiLine1.setRequired(true)
log.warn'It entered'
}

 Below is the screenshot:

Screenshot 2021-03-11 at 9.40.25 AM.png

@Amir Mustapha _Adaptavist_  thanks , I'll take a look :)

@Amir Mustapha _Adaptavist_  it works for me too , Thanks!

I removed the "None" condition since we require to choose a deployment platform.

I saw that you've used logger, how do I use it and where I can see logs in JIRA?

Like Fadoua likes this

Hi @Zachi Meyer ,

Good to know it works for you.

I saw that you've used a logger, how do I use it and where I can see logs in JIRA?      

Adaptavist provides some documentation you can refer to on how to set a log and how to view your log.

Glad to know this is what you are looking for.

Regards,

Amir

Like Fadoua likes this
0 votes
Fadoua Community Leader Feb 11, 2021

@Zachi Meyer 

Since you mentioned behaviour, I assume you have the adaptavist ScriptRunner. If yes I can send you the script.

Please confirm 

Best,

Fadoua

@Fadoua   yes, I have script runner

Fadoua Community Leader Feb 11, 2021

@Zachi Meyer 

Go to the behaviour screen, add the "Deployment Platform" field then click add script:

def deploymentPlatform = getFieldByName("Deployment Platform")

def impact = getFieldByName("Impact")

 

def impactOption = deploymentPlatform.getValue()

If (impactOption == "Mobile"){

  impact.setRequired(true)

}

Else if (impactOption == "xxxx"){

  impact.setRequired(false)

}

 

Make sure to add Else if for all the other options and set them to false otherwise you will get some unexpected behaviour.

Let me know if you have any questions,

Best,

Fadoua

Hi @Fadoua  ,

 

Can I do

 

 Else if (impactOption != "xxxx") ?
Fadoua Community Leader Feb 15, 2021

@Zachi Meyer  Behaviours are little bit different than regular programming, you have to enter each option otherwise I noticed some weird behaviour when I go between options.

There is nothing wrong with trying the code you sent me, however you try to move between the options and see if it is behaving the way you want.

Best,

Fadoua

@Fadoua  the code you gave me and the "Else if" I tried didn't work. The impact field wasn't appeared as required.

I use multi select list, for example:

 

Multi Select.JPG

Fadoua Community Leader Feb 15, 2021

@Zachi Meyer did you add the deployment pattern field to the behaviour? The code should be added to the deployment pattern field because based on the choice there the impact option will be set to required.

Please confirm.

Best,

Fadoua

@Fadoua  By deployment pattern you mean deployment platform field?

I copied you're code and added the other options in the "impactOption" and it didn't work.

Fadoua Community Leader Feb 16, 2021

@Zachi Meyer  Yes Add the deployment platform field to the behaviour? then add the script under the deployment platform field.

Can you please send a screenshot once you hide any sensitive info?

Best,

Fadoua

@Fadoua Yes I add deployment platform field to the behaviour.

I also add mapping.

 

Behaviour STG.jpg

Fadoua Community Leader Feb 17, 2021

@Zachi Meyer  Can you please switch off the use validator plugin?

Also I know my question will sound silly but did you make sure that both fields exist on the screen?

Best,

Fadoua

@Fadoua  I switched off the validator but it still didn't work.

Both fields appears on the same screen (Please see the screenshot):

Deployment Platform screen.jpg

Fadoua Community Leader Feb 17, 2021

@Zachi Meyer 

def deploymentPlatform = getFieldByName("Deployment Platform")

def impactAnalysis = getFieldByName("Impact Analysis")

 

def impactAnalysisOption = deploymentPlatform.getValue()

If (impactAnalysisOption == "Mobile"){

  impactAnalysis.setRequired(true)

}

Else if (impactAnalysisOption == "xxxx"){

  impactAnalysis.setRequired(false)

}

Please update the field to reflect "Impact Analysis"

I am seeing it as the issue otherwise I will ping someone from Adaptavist here

@Fadoua  it's OK . I replaced it before to "impact analysis" and it didn't work either :(

Fadoua Community Leader Feb 17, 2021

@Kristian Walker _Adaptavist_  your help is needed here please, I am not sure what am I missing that the script is not working as expected.

Thank you!

Best,

Fadoua

Fadoua Community Leader Feb 17, 2021

@Zachi Meyer  Let's see if Kristian can help as he/she works for Adaptavist

Hi Fadouha,

Please note that I will help as best as I can but I am not an expert on ScriptRunner for Jira Server.

However, I can confirm when using Select list fields you need to get the value out of the *List* data structure to compare to in your script and I can confirm we have an example located here which shows how to do an operation when a value is selected in a checkbox field which is similar to your requirement and you will be to take this script and use it as a reference guide to help create the script that you require.

If you require assistance with this, then I would advise raising a support ticket for this through our support portal located here so that someone in the ScriptRunner for Jira Server support team can assist yourself with this.

Regards,

Kristian

Fadoua Community Leader Feb 18, 2021

@Kristian Walker _Adaptavist_  thank you very much for your quick assistance! I totally missed the tabs.

@Zachi Meyer Any luck?

@Fadoua  @Kristian Walker _Adaptavist_ Thanks you for assistance.

I didn't have time today to deal with it. I will update you If it will work soon :)

Fadoua Community Leader Feb 18, 2021

@Zachi Meyer I will try as well later tonight.

Hi @Fadoua  and @Kristian Walker _Adaptavist_ ,

I wrote this code like this guide here,

but it still doesn't work ... what am I missing? :(

import com.onresolve.jira.groovy.user.FieldBehaviours
import org.apache.log4j.Logger
import org.apache.log4j.Level
import groovy.transform.BaseScript

def impactAnalysisField = getFieldByName("Impact Analysis");
def deploymentPlatformField = getFieldById(fieldChanged);
def deploymentPlatformFieldValue = deploymentPlatformField.value;


def chosenValuesList = []
if (deploymentPlatformFieldValue in String) {
chosenValuesList.add(deploymentPlatformFieldValue)
} else if (deploymentPlatformFieldValue in ArrayList) {
chosenValuesList.addAll(deploymentPlatformFieldValue)
}

if (chosenValuesList == ['Mobile']) { // if mobile was chosen as deployment platform field
ImpactAnalysisListField.setRequired(true);

// If the user has selected "None" and another value
} else if ('None' in chosenValuesList) {
ImpactAnalysisListField.setRequired(false);

// If the user didn't select any value, or has selected some values, but NOT the "None" value
} else {
ImpactAnalysisListField.setRequired(false);
}

Hi Zachi,

If you are experiencing issues with the code that you have written where it is not working as expected then I would advise you to create a support ticket located in our support portal here, so that my colleagues in our server support team can help advise on why the script is not working as expected.

Regards,

Kristian

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Confluence

Introducing External Collaboration for Confluence

We’re excited to introduce external collaboration for Confluence, now available in early access. It is available to preview for Confluence Cloud Premium and Enterprise customers. (If you're not on ...

199 views 0 7
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