Check mandatory fields by dependencies (creation via API REST call)

Chiara Spadavecchia January 29, 2021

Hi,

I'm using ScriptRunner Validator.

I have 2 fields:

- field A (type: Labels)

- field B (type: Labels)

At least one of the two is mandatory.

 

So, If A is not set check if B is set.

If B is not set the validator should block the transition.

 

How to do this?

The condition into the validator script is the following one:

A!=null || B!=null

and does not work by REST API call.

If I passed B the validator script blocks the transition.

 

1 answer

0 votes
Kristian Walker _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.
January 29, 2021

Hi Chiara,

Thank you for your question.

I can confirm that Workflow Validators inside of ScriptRunner for Jira Cloud are defined using the Jira Expression Framework as documented in the Atlassian API documentation page located here or in the documentation page located here which explains how to add in a new validator to your workflow.

I can confirm that when viewing the Jira Expression Framework API documentation page linked above you can navigate to the Context variables section which shows what variables are provided by this framework that can be used to create the expression.

If you then click on one of the variables it will show all the properties that can be called on the variable for the expression such as for the issue variable as shown here and you will be able to check this to see if Atlassian provides a variable that can be checked against to help create the validator which you require.

I am unable to provide an exact expression to achieve your requirement but you can use the resources shared above as a reference guide to help create that validator expression which you require.

Regards,

Kristian

Chiara Spadavecchia January 29, 2021

I have already read the documentation without finding answers. Can you support me please? Is it clear the problem? It is a very simple case.

Imagine a screen with 2 fields.

One of the two fields is mandatory.

The issue creation By API REST the validator does not work.

The issue craetion by Jira the validator works fine.

Do you know why?

Kristian Walker _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.
January 29, 2021

Hi Chiarra,

Thank you for your response.

I have done a test creating a new issue from the Script Console and can confirm that if when creating an issue that you do not specify a value for one of the fields in the validator in the body of the request to create the issue that the validator will fail and the issue will not be created.

This is how Jira is designed and the Atlassian Rest API's which Jira uses mandate that the validators must pass for the issue to be created as the API's use the same rules as defined in the create transition for the workflow.

This means that you cannot skip this validator logic or override it when creating an issue via a Rest API call as Atlassian does not permit this and this means you must specify the value for one of the fields in the body of the request to create the issue just like you would be required to specify the values if you were creating the issue manually inside of Jira.

I hope this helps,

Kristian

Chiara Spadavecchia January 29, 2021

ok, please can you share the screenshot of the validator configuration? I would like to see the condition (jira expression). Please.

Kristian Walker _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.
January 29, 2021

Hi Chiara,

I can confirm the condition I used for my Jira Expression was the one shown here on line 51.

As for how I created the validator then I did this by using the expressions

Also for the script, I used to test creating an issue from the Script Console this was the one which you can see by going to the script Console page in your instance at the URL of <JiraCloudURL>/plugins/servlet/ac/com.onresolve.jira.groovy.groovyrunner/script-console?s=com.onresolve.jira.groovy.groovyrunner__script-console and then clicking on the link named Create Task in the Examples section below the code box.

Regards,

Kristian

Chiara Spadavecchia January 29, 2021

Ok, could you send me please the REST API call using curl command? 

Chiara Spadavecchia January 29, 2021

For example,

> curl -D- \

-X POST -H "Authorization: Basic XXXXX " \

-H "Content-Type: application/json" \

-H "Accept: application/json" \

--data '{ "fields": {"project": {"key": "XXXXX"}, "summary": "issue1", "customfield_B" : ["label1","label2"] , "issuetype": { "name": "Task" } }}'  XXXXX/rest/api/2/issue

If I don't insert also customfield_A REST API call fails.

Kristian Walker _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.
January 29, 2021

Hi Chiara,

I see you are using a Curl rest call and I cannot advise on how to use Curl as this is not related to ScriptRunner.

I can confirm to see the rest call I used you will need to go to the script Console page in your instance at the URL of <JiraCloudURL>/plugins/servlet/ac/com.onresolve.jira.groovy.groovyrunner/script-console?s=com.onresolve.jira.groovy.groovyrunner__script-console and then clicking on the link named Create Task in the Examples section below the code box as this will populate with the script that contains the rest call.

This requires you to have ScriptRunner for Jira Cloud installed and if you have any questions on the Curl rest call then I would contacting Atlassian support as I am unable to advise on how Curl works.

Regards,

Kristian

Chiara Spadavecchia January 29, 2021

Ok, I used the Script Console. I have the same problem. If I don't specify the value for one of the two fields the validator blocks the transition. Could you send me please the screenshot of the script into Console (where you insert only one of the two fields)?

You said: "I have done a test creating a new issue from the Script Console and can confirm that if when creating an issue that you do not specify a value for one of the fields in the validator in the body of the request to create the issue that the validator will fail and the issue will not be created."

Chiara Spadavecchia January 29, 2021

Your condition into Validator is the following one:

issue.customfield_10099 !=null || issue.customfield_10100 !=null 

Please test the Issue Creation (by Console) adding the value only for the field 10100.

The issue is created?

Please let me know. 

Kristian Walker _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.
January 29, 2021

Hi Chiara,

This is the correct expected behaviour with the expression one of the fields must have a value and if one of the fields does not then the validator will fail and will not allow the issue to be created.

This is how Jira Expressions are designed to work.

Can I ask you to clarify what behaviour are you expecting to happen so that I can confirm if it is possible to achieve this behaviour.

Regards,

Kristian

Chiara Spadavecchia January 29, 2021

It is clear for me what you say but the behaviour is different if the creation is done by REST API or by Jira Screen. By Jira Screen the behaviour is correct: if one of the two fields is valorized the issue is created.

By REST API the behaviour is not the same: If you specify only the 2nd field of the validator condition the issue is not created and the alert is shown.

Please do the test above.

Kristian Walker _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.
January 29, 2021

Hi Chiara,

I have tested this using the condition shown here on line 51 and then I ran the script console script on the script console with just specifying the value for the second field and not the first field and it allowed me to create the issue so it is working as expected from my point of view.

If it is not working in your instance then this indicates there is an issue with how this has been configured or the rest API call and I would contacting Atlassian support to ask them to advise further on why this happening as the Jira Rest API's are provided by Atlassian so I cannot advise on why it is not working as expected.

I can confirm that to set a label field that you need to use a line in the request of customfield_12345: ["ALabel"] to set the label as an Array as Labels fields are arrays and you need to replace the ID of the field with the ID of your Label field.

Regards,

Kristian

Chiara Spadavecchia January 29, 2021

Please help me. It is a very strange behavior in my instance and I finished ideas.

Kristian Walker _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.
January 29, 2021

Hi Chiara,

Please note I am doing my best to help yourself as much as I am able to and I apologise if you do not find my responses helpful.

I can confirm that I have included the script I used here and you will be able to take this script and to use this as an example to see where it worked for me when I used this script along with the validator expression as shown in line 51 of the file located here.

One thing I have noticed from your curl command is you are referring to the fields not using their long ID and I would ask you to use this to refer to the fields.

Regards,

Kristian

Chiara Spadavecchia January 29, 2021

Ok, the name in the curl command was an example. I used the custom field code like you. I repeat all again starting by zero and taking into account your example.
Thanks for your time and support.

Kristian Walker _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.
January 29, 2021

Hi Chirada,

You are welcome.

If this issue has been resolved, can you please mark this answer as accepted so that other users can please mark this example as resolved so that other users can see that this a correct answer.

Regards,

Kristian

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
PREMIUM
TAGS
AUG Leaders

Atlassian Community Events