Forums

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

Making a field required based on another field's answer?

Jane Ellis October 17, 2023

Hello! I am attempting to locate a behavior script for scriptrunner for Jira Cloud.

Use case scenario:

When an issue is set to highest or high priority, we want to require another field to be filled in that is getting missed. I'm hoping this is possible with behaviors for scriptrunner in Jira Cloud, but I have been unsuccessful in writing or locating a script that will do this. 

 

Thanks in advance!

2 answers

1 accepted

3 votes
Answer accepted
Trudy Claspill
Community Champion
October 17, 2023

Hello @Jane Ellis 

Are you trying to set the field to be required while the issue is being created? Or are you talking about the Priority being changed after the issue has been created?

Evaluation of other fields Required state is not evaluated when the issue is being edited since all editing is now done in Jira Cloud inline. Only the field that is being edited can be evaluated during the Edit operation to see if it is required.

Behaviours can be used to conditionally make a field Required only in the Create Issue dialog.

Jane Ellis October 18, 2023

On creation, preferably. I knew it would be an issue during editing. 

Trudy Claspill
Community Champion
October 18, 2023

Did you try a script similar to the example provided in the Behaviours documentation?

https://docs.adaptavist.com/sr4jc/latest/features/behaviours/example-behaviour#make-field-required-when-select-list-value-selected

Jane Ellis December 6, 2023

The issue is that both of the fields we're using are standard jira fields, so I can't call them the way that script does and it's throwing errors when I try to change it.

Trudy Claspill
Community Champion
December 6, 2023

Please show us your changed script and tell us what error you are getting.

Jane Ellis December 6, 2023

.

Jane Ellis December 11, 2023
const changedField = getChangeField();

if(changedField.getType() == "com.atlassian.jira.plugin.system.customfieldtypes:select"
    && changedField.getName() == "Priority"
    && changedField.getValue().value == "Highest") {
   
    getFieldById("fixVersions").setRequired(true);
   
}else{
    getFieldById("fixVersions").setRequired(false);
}
@Trudy Claspill The bold section is where the error lies. I'm just not sure how to tell it to pull a system field instead of a custom field. When I tried it with one of our custom fields, it worked.
Trudy Claspill
Community Champion
December 11, 2023

What error are you getting?

Trudy Claspill
Community Champion
December 11, 2023

Hello @Jane Ellis 

I have tried the same script and a similar script from this Youtube video.

https://www.youtube.com/watch?v=T_22rKXWNrg

In both cases I am getting this error:

"Property 'value' does not exist on type 'string | LabelsField | { accountId: string; } | ComponentsField | { accountId: string; } | IssueTypeField | { version: 1; type: "doc"; content: unknown[]; } | ... 9 more ... | { ...; }'. Property 'value' does not exist on type 'string'."

The documentation and the video are the only references I have seen that are applicable to Jira Cloud.

In absence of other references, I recommend that you reach out to Adaptavist support directly about this. Their contact information can be found in their Marketplace listing:

https://marketplace.atlassian.com/apps/1228522/behaviours-scriptrunner-for-jira-cloud?hosting=cloud&tab=support

 

It would be great if you report back here what you learn, for the benefit of other members of the community.

Like Jane Ellis likes this
Jane Ellis December 11, 2023

I’m not getting any errors. That’s the thing! It’s just not doing what I want it to do. :( I know it’s because priority isn’t a custom field, but every time I’ve changed it to what I think it should be, it still doesn’t work. 

If you remove the .value, it still doesn’t work. It’s super annoying. 

Trudy Claspill
Community Champion
December 11, 2023

I don't think it has to do with Priority not being a custom field.

I think my response with the video link got posted at about the same time you were posting your reply, so you probably hadn't seen it yet.

Following the video I linked above, I removed the line about checking the changed field type.

I tried using both "value" and "name" as the attributes after "getValue()" and got the same error message.

Screenshot 2023-12-11 at 5.16.53 PM.png

 

I also tried adding logger.info statements, but the Logs page for ScriptRunner shows no logs found for Behaviours.

Jane Ellis December 13, 2023

@Trudy Claspill ! Thank you so much for that youtube video!! It was a HUGE help! In piecing the two scripts together (the one from your initial comment), I was able to accomplish what I needed!!! See below (I know there is an error, but it still works!):

Screenshot 2023-12-13 122016.png

Like Trudy Claspill likes this
Trudy Claspill
Community Champion
December 13, 2023

That is GREAT!!

It is good to know that even though the code shows an error, it actually executes correctly.

If you found my responses to be helpful in solving your problem, please consider clicking the Accept Answer button. That will help others searching the community find posts with details of working solutions.

0 votes
Crystal Baker October 25, 2023

I have accomplished this using jira workflow conditions and validators.

If my condition checkbox called "needs ops review" is checked, then the field "ops reviewer" is required (validator).
 
image (8).png

Crystal Baker October 25, 2023

I should clarify -- if they don't provide the required field then the workflow step won't get completed (in your case, "create issue." 

Jane Ellis December 11, 2023

@Crystal Baker it looks like you're achieving this with an app, JSU. I don't have the ability to purchase that right now and already have scriptrunner installed. The conditions and validators "out of the box" don't work the same way.

Crystal Baker December 12, 2023

Ah, I had no idea. Thanks

Suggest an answer

Log in or Sign up to answer